unable to create directory in external storage

风格不统一 提交于 2019-12-23 02:02:04

问题


I wanted to the application to create a new directory to store downloaded image inside sdcard. I tried a few methods but still I can't seems to get it work. I included permission for External Storage in android manifest as well. Any comments will be appreciated.

        Bitmap bm = null;
        InputStream in;
            in = new java.net.URL("http://blogs.computerworld.com/sites/computerworld.com/files/u177/google-nexus-4.jpg").openStream();
            bm = BitmapFactory.decodeStream(new PatchInputStream(in));
            File file = new File(Environment.getExternalStorageDirectory() + "/map");
            file.mkdirs();
            File outputFile = new File(file, "Image"+NumIncrease);
            FileOutputStream fos = new FileOutputStream(outputFile);

Below is what I get from running this.

12-05 15:55:25.566: D/skia(4244): ---- read threw an exception
12-05 15:55:25.585: D/skia(4244): ---- read threw an exception
12-05 15:55:25.585: W/System.err(4244): java.io.IOException: Is a directory
12-05 15:55:25.585: W/System.err(4244):     at org.apache.harmony.luni.platform.OSFileSystem.read(Native Method)
12-05 15:55:25.585: W/System.err(4244):     at dalvik.system.BlockGuard$WrappedFileSystem.read(BlockGuard.java:165)
12-05 15:55:25.585: W/System.err(4244):     at java.io.FileInputStream.read(FileInputStream.java:290)
12-05 15:55:25.585: W/System.err(4244):     at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:166)
12-05 15:55:25.863: E/MediaStore(4244): Failed to create thumbnail, removing original

EDIT Below is how I handle the bitmap.

bm.compress(Bitmap.CompressFormat.JPEG, 85, fos);

回答1:


Following points may help you

  • Probably exception is thrown because there is no MediaCard subdir. You should check if all dirs in the path exist.
  • Add the permissions in the manifest for this to work..
  • You should never use "/sdcard/" as a constant, because manufacturers may decide to mount the SD-Card to another path. Always use Environment.getExternalStorageDirectory(); instead.

Further reading

  • http://developer.android.com/reference/android/Manifest.permission.html#WRITE_EXTERNAL_STORAGE
  • Android saving file to external storage
  • Android write to sd card folder


来源:https://stackoverflow.com/questions/13718658/unable-to-create-directory-in-external-storage

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