保存图片给出了FileNotFoundException()

时间:2016-07-26 18:02:54

标签: android file

我是Android相机API的新手,在代码中调用了 PictureCallback

     PictureCallback jpegCallback = new PictureCallback() {
          public void onPictureTaken(byte[] data, Camera camera) {
             // Save the image JPEG data to the SD card
             FileOutputStream outStream = null;
             try {
                String path = Environment.getExternalStorageDirectory() +"\test.jpg";

            outStream = new FileOutputStream(path);
            outStream.write(data);
            outStream.close();
         } catch (FileNotFoundException e) {
            Log.e(TAG, "File Note Found", e);
         } catch (IOException e) {
            Log.e(TAG, "IO Exception", e);
         }
      }
   };

该行的异常指出:

outStream = new FileOutputStream(path);

由于我是Android界的新手,我不知道这个 Environment.getExternalStorageDirectory()指出了哪里。

例外说:

  

Java.io.FileNotFoundException:/ storage / emulated / 0 est.jpg:open   失败:EACCES(权限被拒绝)

但在我的清单中:

<uses-feature android:name="android.hardware.Camera"/>
   <uses-permission android:name="android.permission.CAMERA"/>

编辑:我修正了路径

String path = Environment.getExternalStorageDirectory() +"/test.jpg";

但仍然给了我:

  

java.io.FileNotFoundException:/storage/emulated/0/test.jpg:open   失败:EACCES(权限被拒绝)

2 个答案:

答案 0 :(得分:2)

  

java.io.FileNotFoundException:/storage/emulated/0/test.jpg:open   失败:EACCES(权限被拒绝)

你一定忘了添加

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

答案 1 :(得分:1)

Android使用linux。路径分隔符为/而非\。如果您想要实际使用\t \

\\t也会添加一个标签