Save image to sdcard from drawable resource on Android
The process of saving a file (which is image in your case) is described here: save-file-to-sd-card
Saving image to sdcard from drawble resource:
Say you have an image namely ic_launcher in your drawable. Then get a bitmap object from this image like:
Bitmap bm = BitmapFactory.decodeResource( getResources(), R.drawable.ic_launcher);
The path to SD Card can be retrieved using:
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
Then save to sdcard on button click using:
File file = new File(extStorageDirectory, "ic_launcher.PNG");
FileOutputStream outStream = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
Don't forget to add android.permission.WRITE_EXTERNAL_STORAGE
permission.
Here is the modified file for saving from drawable: SaveToSd
, a complete sample project: SaveImage
Save images from drawable to internal file storage in Android
Saving image to sdcard from drawble resource:
Say you have an image namely ic_launcher
in your drawable. Then get a bitmap object from this image like:
Bitmap bm = BitmapFactory.decodeResource( getResources(), R.drawable.ic_launcher);
The path to SD Card can be retrieved using:
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
Then save to sdcard on button click using:
File file = new File(extStorageDirectory, "ic_launcher.PNG");
outStream = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
Don't forget to add android.permission.WRITE_EXTERNAL_STORAGE
permission.
How to save image from drawable resource to sd card?
String folder = "/sdcard/Pictures/MyAppFolder";
void save(){
Imageview view = (ImageView)findViewById(R.id.cachesView);
view.buildDrawingCache();
Bitmap yourBitmap = view.getDrawingCache();
final File myDir = new File(folder);
myDir.mkdirs();
final Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
final String fname = "StyleMe-" + n + ".png";
File file = new File(myDir, fname);
if (file.exists()) {
FileOutputStream out = new FileOutputStream(file);
yourBitmap.compress(CompressFormat.JPEG, 100, out);
out.flush();
out.close();
sendBroadcast(
new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://"+ Environment.getExternalStorageDirectory()))
); // this will refresh the gallery app.
Toast.makeText(getApplication(), "Image Saved", Toast.LENGTH_SHORT).show();
}
}
please see
add this method in your class and on click button put
save();
Numbering of image saved from app resource to SD card
From your question and comments i can understand that you want to save n number of images to SDCard.
To save follow the steps
STEP 1: Get All the Images you need. Make sure you getting the list of images correctly here.
STEP 2: Count number of images in the list and store it in variale
int numberOfImages = 15;// Get it dynamically
STEP 3: Now loop it to store all the images in sequential order
//Create Directory to store images in SDCard
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
if(!myDir.exists()){
myDir.mkdirs();
}
// You have to get next image here from the resource here
bm = BitmapFactory.decodeResource( mContext.getResources(), images[i]);// value for itemPos should be given here.
// Get Last Saved Number
SharedPreferences savedNumber = getSharedPreferences(PREFS_NAME, 0);
int lastSavedNumber = savedNumber.getInt("lastsavednumber",0);
lastSavedNumber++;
String fname = "Image-"+lastSavedNumber+".png";
File file = new File (myDir, fname);
if (file.exists ()) {file.delete (); }
try {
FileOutputStream out = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.JPEG, 90, out);//Your Bitmap from the resouce
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
//To Store the last Number
SharedPreferences saveNumber = getApplicationContext().getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editorset = saveNumber.edit();
editorset.putInt("lastsavednumber",lastSavedNumber);
editorset.commit();
The duplication can take place if you do any thing wrong in your first step.
EDIT
To Store All Images in Sequential Order Use SharedPreferences to Store last saved image number.
public static final String PREFS_NAME = "ImageNumber";
// Get Last Saved Number
SharedPreferences savedNumber = getSharedPreferences(PREFS_NAME, 0);
int lastSavedNumber = savedNumber.getInt("lastsavednumber",0);
lastSavedNumber++;
String fname = "Image-"+lastSavedNumber+".png";
//To Store Last Saved Number
SharedPreferences saveNumber = getApplicationContext().getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editorset = saveNumber.edit();
editorset.putInt("lastsavednumber",lastSavedNumber);
editorset.commit();
Android Studio: save image in SD Card
First, you need to get your Bitmap. You can already have it as an object Bitmap, or you can try to get it from the ImageView such as:
BitmapDrawable drawable = (BitmapDrawable) mImageView1.getDrawable();
Bitmap bitmap = drawable.getBitmap();
Then you must get to directory (a File object) from SD Card such as:
File sdCardDirectory = Environment.getExternalStorageDirectory();
Next, create your specific file for image storage:
File image = new File(sdCardDirectory, "test.png");
After that, you just have to write the Bitmap thanks to its method compress such as:
boolean success = false;
// Encode the file as a PNG image.
FileOutputStream outStream;
try {
outStream = new FileOutputStream(image);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
/* 100 to keep full quality of the image */
outStream.flush();
outStream.close();
success = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Finally, just deal with the boolean result if needed. Such as:
if (success) {
Toast.makeText(getApplicationContext(), "Image saved with success",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"Error during image saving", Toast.LENGTH_LONG).show();
}
Don't forget to add the following permission in your Manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Save Image from drawable to sdcard in camera activity
You can save photos in Android without the need for an OutputStream and all the other logic that accompanies it. Here is a simple recipe which will do what I think you are trying to accomplish. Pay special attention to the Intent and how it is used to set up the saving of the image, as this is I think where you are going wrong.
public class PhotoActivity extends Activity implements OnClickListener {
private Button takePicture;
private String path;
private File imageFile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
takePicture = (Button) findViewById(R.id.button1);
takePicture.setOnClickListener(this);
path = Environment.getExternalStorageDirectory() + "/my_image.png";
imageFile = new File(path);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri uri = Uri.fromFile(imageFile);
i.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivity(i);
break;
}
}
}
Related Topics
Change the Text Color of Numberpicker
Android: Programmatically Detect If Device Has Hardware Menu Button
Android Onclick in Xml VS. Onclicklistener
Creating Custom Lockscreen in Android
How to Crop an Image in Android
Ndk Resolution Outcome: Project Settings: Gradle Model Version=5.4.1, Ndk Version Is Unknown Error
How to Save Image in Android Gallery
Android Java.Lang.Noclassdeffounderror
How to Create a Custom Notification Layout in Android
Googlesignatureverifier Signature Not Valid Message (Google Play Services 9.0.0)
How to Manipulate the Camera Preview
Android: Cursoradapter, Listview and Checkbox
Google Play Services Out of Date. Requires 3159100 But Found 3158130
Android Studio Adb Syntax Error: ")" Unexpected
Onrequestpermissionsresult Not Being Called in Fragment If Defined in Both Fragment and Activity