Search code examples
androidfilebitmapsd-card

Can't create file in sd-card


I am trying to save a bitmap to a file and then saving it on sd-card. Here's my code:

String path = Environment.getExternalStorageDirectory().toString();
File file = new File(path, "Jhs"+".jpg");
file.mkdir();
OutputStream fOut;
try {
    fOut = new FileOutputStream(file);
    BitmapMatrix convMatrix = new BitmapMatrix(0);
    convMatrix.result.compress(Bitmap.CompressFormat.JPEG, 100, fOut);

    fOut.flush();
    fOut.close();
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Also this code is in a class which does not extend Activity.I am just giving this information thinking that maybe there is some issue with this. Here's my LogCat

05-01 18:00:22.555: W/System.err(31392): java.io.FileNotFoundException:           /mnt/sdcard/Jhs.jpg (Permission denied)
05-01 18:00:22.555: W/System.err(31392):    at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
05-01 18:00:22.555: W/System.err(31392):    at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)
05-01 18:00:22.555: W/System.err(31392):    at java.io.FileOutputStream.<init>(FileOutputStream.java:94)
05-01 18:00:22.555: W/System.err(31392):    at java.io.FileOutputStream.<init>(FileOutputStream.java:66)
05-01 18:00:22.555: W/System.err(31392):    at com.example.imageprocess.BitmapMatrix.computeConvolution3x3(BitmapMatrix.java:123)
05-01 18:00:22.555: W/System.err(31392):    at com.example.imageprocess.MainActivity.sharpen(MainActivity.java:105)
05-01 18:00:22.555: W/System.err(31392):    at com.example.imageprocess.MainActivity.onCreate(MainActivity.java:51)
05-01 18:00:22.555: W/System.err(31392):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
05-01 18:00:22.555: W/System.err(31392):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836)
05-01 18:00:22.555: W/System.err(31392):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893)
05-01 18:00:22.555: W/System.err(31392):    at android.app.ActivityThread.access$1500(ActivityThread.java:135)
05-01 18:00:22.555: W/System.err(31392):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054)
05-01 18:00:22.555: W/System.err(31392):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-01 18:00:22.565: W/System.err(31392):    at android.os.Looper.loop(Looper.java:150)
05-01 18:00:22.565: W/System.err(31392):    at android.app.ActivityThread.main(ActivityThread.java:4389)
05-01 18:00:22.565: W/System.err(31392):    at java.lang.reflect.Method.invokeNative(Native Method)
05-01 18:00:22.565: W/System.err(31392):    at java.lang.reflect.Method.invoke(Method.java:507)
05-01 18:00:22.565: W/System.err(31392):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
05-01 18:00:22.565: W/System.err(31392):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
05-01 18:00:22.565: W/System.err(31392):    at dalvik.system.NativeStart.main(Native Method)

Solution

  • That logcat is saying you don't have permission to write the file, Make sure you have declared

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

    in the android manifest.

    You can add this:

    File dir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath()+ "/%YOUR_FOLDER%/");
    if (!(dir.exists() && dir.isDirectory())) {
        dir.mkdirs();
    }
    

    above your current code if you want to try a different folder location for the file and see if that helps. The code will create the directory /sdcard/Downloads/YOUR_FOLDER/ in which you can create the file. See here for storage location options.

    You can then do

    File file = new File(dir, "Jhs"+".jpg");
    file.createNewFile(); //CREATE THE FILE FIRST!
    OutputStream fOut;
    try {
        fOut = new FileOutputStream(file);
        ....
    }
    

    etc as in your post.