Can't create file in sd-card

冷暖自知 提交于 2019-12-24 00:45:16

问题


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)

回答1:


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.




回答2:


Given the exception you are getting, you need to your AndroidManifest.xml file the WRITE_EXTERNAL_STORAGE permission

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



回答3:


java.io.FileNotFoundException: /mnt/sdcard/Jhs.jpg (Permission denied)

Add this permission in Manifest

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



回答4:


 java.io.FileNotFoundException: /mnt/sdcard/Jhs.jpg (Permission denied)

It looks like you don't have WRITE_EXTERNAL_STORAGE perimission.

Add this to your AndroidMAnifest.xml:

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


来源:https://stackoverflow.com/questions/16318196/cant-create-file-in-sd-card

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!