I take a photo and show it in an imageview. Then I get the bitmap from the imageview in my activity and when I press a button I want to save this bitmap into phone's gallery. What should I do?
Asked
Active
Viewed 1.2k times
3 Answers
8
call this function in Button onClick
private void saveImage() {
File myDir=new File("/sdcard/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
check this save bitmap

Community
- 1
- 1

RajaReddy PolamReddy
- 22,428
- 19
- 115
- 166
1
Try this
Bitmap toDisk = Bitmap.createBitmap(w1,h1,Bitmap.Config.ARGB_8888);
setBitmap(toDisk);
Bitmap myBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ban_background);
Bitmap resizeImage1=Bitmap.createScaledBitmap(myBitmap,590,350,false);
try {
toDisk.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File("/mnt/sdcard/image".jpg")));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Mercy
- 1,862
- 9
- 39
- 75
-
7-1 for undocumented, bad formatted, unexplained code-only answer. – WarrenFaith Jan 03 '12 at 11:48
0
You will have to get the path to phone's External Storage directory first, then the directory where gallery images are stored. On most Android models this is /mnt/sdcard/pictures but I do not suggest hardcoding this path instead use
Environment.getExternalStorageDirectory();
After that just create a File path to that directory and use an outputStream to write your bitmap to that directory.

Jade Byfield
- 4,668
- 5
- 30
- 41