I have a small piece of code that just won't write a file to the SD card on Android.
InputStream in = thumbnailUrl.openStream();
File outputFile = new File(CACHE_DIR, photo.getId());
if (!outputFile.canWrite()) {
throw new IOException("Can't write to this file");
}
FileOutputStream fos = new FileOutputStream(outputFile);
BufferedOutputStream bos = new BufferedOutputStream(fos, 1024);
byte[] data = new byte[1024];
int datapart;
while ((datapart = in.read(data, 0, 1024)) >= 0) {
bos.write(data, 0, datapart);
}
bos.close();
fos.close();
in.close();
I 开发者_开发知识库made sure I created the folder CACHE_DIR
before. It exists on the SD card, but canWrite()
always returns false
. This is the path: /mnt/sdcard/Android/data/org.my.app/files/thumbnailcache
I can browse the directory /mnt/sdcard/Android/data/org.my.app/files/thumbnailcache
.
Here comes the interesting part: When I try to write to /mnt/sdcard/Test/
, it succeeds.
How is that possible?
do you have the
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
permission?
精彩评论