UPDATED
I want to save bitmap image to the SD card/device gallery. I tried THIS and THIS . Code is working and i am checking it on emulator. Whenever i save the image, there is nothing in the gallery. Please help
Log Report
07-25 02:57:34.345: E/MainActivity(1249): Error in creating fragment
07-25 02:57:35.476: I/Choreographer(1249): Skipped 95 frames! The application may be doing too much work on its main thread.
07-25 02:57:51.955: D/dalvikvm(1249): GC_FOR_ALLOC freed 1409K, 21% free 5888K/7400K, paused 46ms, total 73ms
07-25 02:57:51.955: I/dalvikvm-heap(1249): Grow heap (frag case) to 6.959MB for 1166416-byte allocation
07-25 02:57:52.085: D/dalvikvm(1249): GC_FOR_ALLOC freed 44K, 6% free 6983K/7400K, paused 53ms, total 53ms
07-25 02:57:52.266: I/Choreographer(1249): Skipped 60 frames! The application may be doing too much work on its main thread.
07-25 02:57:55.556: D/dalvikvm(1249): GC_FOR_ALLOC freed 2621K, 34% free 5702K/8568K, paused 42ms, total 44ms
07-25 02:57:55.606: W/System.err(1249): java.io.FileNotFoundException: /storage/sdcard/MyFolder/storage/sdcard/MyFolder/ pic.jpg: open failed: ENOENT (No such file or directory)
07-25 02:57:55.606: W/System.err(1249): at libcore.io.IoBridge.open(IoBridge.java:409)
07-25 02:57:55.606: W/System.err(1249): at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
07-25 02:57:55.616: W/System.err(1249): at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
07-25 02:57:55.616: W/System.err(1249): at com.example.navigationexample.MainActivity.saveImage(MainActivity.java:314)
07-25 02:57:55.626: W/System.err(1249): at com.example.navigationexample.MainActivity.onOptionsItemSelected(MainActivity.java:148)
07-25 02:57:55.626: W/System.err(1249): at android.app.Activity.onMenuItemSelected(Activity.java:2566)
07-25 02:57:55.639: W/System.err(1249): at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:372)
07-25 02:57:55.639: W/System.err(1249): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:986)
07-25 02:57:55.646: W/System.err(1249): at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
07-25 02:57:55.646: W/System.err(1249): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
07-25 02:57:55.646: W/System.err(1249): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
07-25 02:57:55.658: W/System.err(1249): at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:547)
07-25 02:57:55.658: W/System.err(1249): at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:115)
07-25 02:57:55.658: W/System.err(1249): at android.view.View.performClick(View.java:4240)
07-25 02:57:55.666: W/System.err(1249): at android.view.View$PerformClick.run(View.java:17721)
07-25 02:57:55.676: W/System.err(1249): at android.os.Handler.handleCallback(Handler.java:730)
07-25 02:57:55.676: W/System.err(1249): at android.os.Handler.dispatchMessage(Handler.java:92)
07-25 02:57:55.676: W/System.err(1249): at android.os.Looper.loop(Looper.java:137)
07-25 02:57:55.686: W/System.err(1249): at android.app.ActivityThread.main(ActivityThread.java:5103)
07-25 02:57:55.695: W/System.err(1249): at java.lang.reflect.Method.invokeNative(Native Method)
07-25 02:57:55.695: W/System.err(1249): at java.lang.reflect.Method.invoke(Method.java:525)
07-25 02:57:55.695: W/System.err(1249): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
07-25 02:57:55.706: W/System.err(1249): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-25 02:57:55.706: W/System.err(1249): at dalvik.system.NativeStart.main(Native Method)
07-25 02:57:55.716: W/System.err(1249): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
07-25 02:57:55.726: W/System.err(1249): at libcore.io.Posix.open(Native Method)
07-25 02:57:55.726: W/System.err(1249): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
07-25 02:57:55.726: W/System.err(1249): at libcore.io.IoBridge.open(IoBridge.java:393)
07-25 02:57:55.736: W/System.err(1249): ... 23 more
07-25 02:57:56.056: I/Choreographer(1249): Skipped 32 frames! The application may be doing too much work on its main thread.
Save function public void saveImage(Bitmap bitmap) {
String fname = path + " pic" + ".jpg";
File file = new File (dir, fname);
//if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
image.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Function to make folder in SD Card. I have called this after setContentView and call saveImage function when i want to save it
public void makeFolderInSdCard() {
path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyFolder/";
dir = new File(path);
if(!dir.exists())
dir.mkdirs();
}