Android : Save Image from res/drawable to Image Gallery Android

on Monday, September 8, 2014


I want to Save Image from res/drawable to Image Gallery. I am using following code but it is doing nothing.



What is the wrong with my code ? String Drawable stands for Image Name which is there in drawable folder.



File direct = new File(Environment.getExternalStorageDirectory()
+ "/Images");

if (!direct.exists()) {
direct.mkdirs();
}

ByteArrayOutputStream bos = null;
FileOutputStream fos = null;

try {
Bitmap bitmap = BitmapFactory.decodeResource(
context.getResources(),
context.getResources().getIdentifier(
"@drawable/" + Drawable, "drawable",
context.getPackageName()));

bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 95, bos);

byte[] bitmapdata = bos.toByteArray();
fos = new FileOutputStream(direct + "/" + "IMG-" + CurrentDateTime
+ ".jpg");
fos.write(bitmapdata);
} catch (Exception e) {
Log.e("Internal Image Save Error->", e.toString());
} finally {
try {
if (bos != null) {
bos.close();
}
if (fos != null) {
fos.close();
fos.flush();
}
} catch (IOException ignored) {
Log.e("Internal Image Save Error->", ignored.toString());
}
}

0 comments:

Post a Comment