本文介绍了无法在外部存储中创建目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要应用程序创建一个新的目录来存储sdcard中的下载映像。我尝试了几种方法,但我似乎无法让它工作。我还在Android清单中包含了外部存储的许可。任何意见将不胜感激。
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
编辑
以下是我如何处理位图。
EDITBelow is how I handle the bitmap.
bm.compress(Bitmap.CompressFormat.JPEG, 85, fos);
推荐答案
以下几点可能有助于您
- 可能会抛出异常,因为没有MediaCard子目录。
您应该检查路径中是否存在所有目录。 - 将清单中的权限添加到此工作中。
- 您不应该使用/ sdcard /作为常量,因为制造商
可能决定将SD卡安装到另一个路径。总是使用
Environment.getExternalStorageDirectory();
进一步阅读
- a href =http://developer.android.com/reference/android/Manifest.permission.html#WRITE_EXTERNAL_STORAGE =nofollow noreferrer> http://developer.android.com/reference/android/Manifest.permission .html#WRITE_EXTERNAL_STORAGE
- http://developer.android.com/reference/android/Manifest.permission.html#WRITE_EXTERNAL_STORAGE
- Android saving file to external storage
- Android write to sd card folder
这篇关于无法在外部存储中创建目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!