在我的Android应用程序中,我想将图像存储到外部存储。我有这段代码:
package async;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import http.Network;
import interfaces.ImageDownloader;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.ImageView;
public class Async_image_task extends AsyncTask<String, Void, Bitmap> {
ImageDownloader callerContext;
ImageView calledImageView;
String link;
public Async_image_task(ImageView calledImageView, String link) {
this.callerContext = (ImageDownloader)calledImageView.getContext();
this.calledImageView = calledImageView;
this.link = link;
execute(link);
}
@Override
protected Bitmap doInBackground(String... params) {
Bitmap bitmap = Network.DownloadImage((Context) callerContext, params[0]);
if (bitmap != null) {
try {
String[] segments = link.split("/");
int len = segments.length;
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
String imgpath = segments[len-6] + "/" + segments[len-2] + "/" + segments[len-1];
File file = new File(path, imgpath);
file.getParentFile().mkdirs();
String abs = file.getAbsolutePath();
OutputStream fOut = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
MediaStore.Images.Media.insertImage(((Activity)callerContext).getContentResolver(),abs,file.getName(),file.getName());
} catch (Exception e) {
Log.d("IMAGE_ERROR", e.getMessage());
}
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
callerContext.ImageCallback(bitmap, calledImageView);
}
}
在清单中我有这个许可:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
但是当它尝试执行OutputStream fOut = new FileOutputStream(file);
时,它会抛出一个找不到文件的异常....
错误:
06-18 17:48:35.573: D/IMAGE_ERROR(9316): /storage/sdcard0/arin/53/image.jpg: open failed: EACCES (Permission denied)
当我获得许可时,为什么会发生这种情况?
有人知道吗?
由于
logcat的
06-18 18:03:57.522: W/System.err(14213): java.io.FileNotFoundException: /storage/sdcard0/arin/53/image.jpg: open failed: EACCES (Permission denied)
06-18 18:03:57.532: W/System.err(14213): at libcore.io.IoBridge.open(IoBridge.java:416)
06-18 18:03:57.532: W/System.err(14213): at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
06-18 18:03:57.532: W/System.err(14213): at java.io.FileOutputStream.<init>(FileOutputStream.java:128)
06-18 18:03:57.532: W/System.err(14213): at java.io.FileOutputStream.<init>(FileOutputStream.java:117)
06-18 18:03:57.532: W/System.err(14213): at async.Async_image_task.saveImage(Async_image_task.java:64)
06-18 18:03:57.532: W/System.err(14213): at async.Async_image_task.doInBackground(Async_image_task.java:37)
06-18 18:03:57.532: W/System.err(14213): at async.Async_image_task.doInBackground(Async_image_task.java:1)
06-18 18:03:57.532: W/System.err(14213): at android.os.AsyncTask$2.call(AsyncTask.java:287)
06-18 18:03:57.532: W/System.err(14213): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
06-18 18:03:57.532: W/System.err(14213): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-18 18:03:57.532: W/System.err(14213): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
06-18 18:03:57.532: W/System.err(14213): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
06-18 18:03:57.542: W/System.err(14213): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
06-18 18:03:57.542: W/System.err(14213): at java.lang.Thread.run(Thread.java:856)
06-18 18:03:57.542: W/System.err(14213): Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied)
06-18 18:03:57.542: W/System.err(14213): at libcore.io.Posix.open(Native Method)
06-18 18:03:57.542: W/System.err(14213): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
06-18 18:03:57.542: W/System.err(14213): at libcore.io.IoBridge.open(IoBridge.java:400)
06-18 18:03:57.542: W/System.err(14213): ... 13 more
答案 0 :(得分:0)
您尝试创建文件的目录可能不存在。在致电new FileOutputStream
之前尝试此行:
file.mkdirs();
答案 1 :(得分:0)
检查运行sdk版本是否低于lollipop或者小于此值:
File sdcard = Environment.getExternalStorageDirectory();
if (sdcard != null) {
File mediaDir = new File(sdcard, "DCIM/Camera");
if (!mediaDir.exists()) {
mediaDir.mkdirs();
}