我正在尝试在Android应用程序中保存相机拍摄的照片。我使用AsyncTask类来完成这项工作,但是在onBackground函数中抛出异常 - FileNotFoundException。即使我对清单中的外部存储具有读写权限,也会发生这种情况。
private class SaveImageTask extends AsyncTask<byte[], Void, String> {
@Override
protected String doInBackground(byte[]... data) {
FileOutputStream outStream = null;
String url = "example";
try {
System.out.println("x");
File dir = new File (Environment.getExternalStorageDirectory() + "/camtest/");
System.out.println(dir);
if(!dir.exists()) dir.mkdir();
Locale locale = new Locale("en");
String fileName = String.format(locale, "%d.jpg", System.currentTimeMillis());
File outFile = new File(dir, fileName);
System.out.println(outFile.getAbsolutePath());
outStream = new FileOutputStream(outFile);
outStream.write(data[0]);
outStream.flush();
outStream.close();
System.out.println("x");
refreshGallery(outFile);
Toast.makeText(getApplicationContext(), "try", Toast.LENGTH_LONG).show();
url = outFile.getAbsolutePath();
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("File not found");
} catch (IOException e) {
e.printStackTrace();
System.out.println("IO exception");
} finally {
}
return url;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
locationToSave.setPhotoURL(result);
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
}
我的输出日志如下:
01-02 16:53:12.692: I/System.out(16460): x
01-02 16:53:12.692: I/System.out(16460): /mnt/sdcard/camtest
01-02 16:53:12.692: I/System.out(16460): /mnt/sdcard/camtest/1420213992701.jpg
01-02 16:53:12.722: I/System.out(16460): File not found
显然,在尝试编写文件时会跳转到catch块。在我看来,这条路也很好。
有什么想法吗?
编辑:堆栈跟踪
01-02 17:18:48.300: W/System.err(18516): java.io.FileNotFoundException: /mnt/sdcard/camtest/1420215528309.jpg: open failed: EACCES (Permission denied)
01-02 17:18:48.300: W/System.err(18516): at libcore.io.IoBridge.open(IoBridge.java:406)
01-02 17:18:48.300: W/System.err(18516): at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
01-02 17:18:48.300: W/System.err(18516): at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
01-02 17:18:48.300: W/System.err(18516): at michal.myapp.activities.CameraActivity$SaveImageTask.doInBackground(CameraActivity.java:244)
01-02 17:18:48.300: W/System.err(18516): at michal.myapp.activities.CameraActivity$SaveImageTask.doInBackground(CameraActivity.java:1)
01-02 17:18:48.300: W/System.err(18516): at android.os.AsyncTask$2.call(AsyncTask.java:264)
01-02 17:18:48.300: W/System.err(18516): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
01-02 17:18:48.300: W/System.err(18516): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-02 17:18:48.300: W/System.err(18516): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
01-02 17:18:48.300: W/System.err(18516): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
01-02 17:18:48.300: W/System.err(18516): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
01-02 17:18:48.300: W/System.err(18516): at java.lang.Thread.run(Thread.java:864)
01-02 17:18:48.300: W/System.err(18516): Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied)
01-02 17:18:48.310: W/System.err(18516): at libcore.io.Posix.open(Native Method)
01-02 17:18:48.310: W/System.err(18516): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
01-02 17:18:48.310: W/System.err(18516): at libcore.io.IoBridge.open(IoBridge.java:390)
01-02 17:18:48.310: W/System.err(18516): ... 11 more