0

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();
}
Community
  • 1
  • 1
user3864752
  • 143
  • 3
  • 15

5 Answers5

0

have you added the permission in manifest file?

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
SWAG
  • 37
  • 7
0

Create a folder in your sd card after **setContentView()**as follows:

String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyFolder/";
File dir = new File(path);
if(!dir.exists())
    dir.mkdirs();

Then create a new file as follows:

String s = path + "pic" + ".jpg";
File newfile = new File(s);

Then use the FileOutputStream to store your image. Hope this helps. Also, why did you use file.delete()?

Srijith
  • 1,695
  • 17
  • 29
0

// Instead of this

   String fname = path + " pic" + ".jpg";
   File file = new File (dir, fname);

// use this

String fname = "pic" + ".jpg";
File file = new File (dir , fname);
Techfist
  • 4,314
  • 6
  • 22
  • 32
0

Check your log file properly. It is showing "java.io.FileNotFoundException: /storage/sdcard/MyFolder/storage/sdcard/MyFolder/ pic.jpg".

Here "/storage/sdcard/MyFolder" is repeated, which looks like invalid path. So Valid path should be "/storage/sdcard/MyFolder/pic.jpg".

Kadari
  • 311
  • 1
  • 3
  • 5
-1

You need to set of permissions and features in the AndroidManifest.xml file. Add following in your AndroidManifest.xml

  • WRITE_EXTERNAL_STORAGE – Required to store images

it should look like this:

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="17" />

<!-- Accessing camera hardware -->
<uses-feature android:name="android.hardware.camera" />

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

....

then you need a directory name to store your captured image

private static final String IMAGE_DIRECTORY_NAME = "name of your directory";

then a file url to store image

private Uri fileUri;

Also you going to use MediaStore.EXTRA_OUTPUT to specify a path where the image has to be stored sample code below:

intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

click here for more future reference

ziah299
  • 17
  • 2
  • 7