WRITE_EXTERNAL_STORAGE不在棒棒糖上工作,即使它已在清单中设置

时间:2017-01-09 16:15:55

标签: android permissions android-external-storage

我正在尝试将图片从应用本地数据文件夹保存到外部存储。我的清单包含以下内容(在清单的应用程序标记之前):

<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="23" />

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

当我尝试以下

try {
        InputStream in = new FileInputStream(filePath);
        File outPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        File outFile = new File(outPath, "mypicture.jpg");


        //try fails at this line
        OutputStream out = new FileOutputStream(outFile);

        byte[] buf = new byte[1024];
        int len;

        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }

        in.close();
        in = null;
        out.flush();
        out.close();
        out = null;
} catch (IOException e) {
    e.printStackTrace();
}

我收到此错误:

java.io.FileNotFoundException: /storage/emulated/0/Pictures/mypicture.jpg: open failed: EACCES (Permission denied)

我也尝试了稍微不同的输出路径:

 String sdCardPath = Environment.getExternalStorageDirectory() + "/MyFolder";
 new File(sdCardPath).mkdirs();
 File outFile = new File(sdCardPath, "mypicture.jpg");

但这也给了我一个错误:

java.io.FileNotFoundException: /storage/emulated/0/MyFolder/mypicture.jpg: open failed: ENOENT (No such file or directory)

设备运行的是Android 4.4.2,因此不需要在运行时请求权限(据我所知,无法请求它们)。

为了将文件保存到外部存储器,是否还有其他可能缺失的内容?

4 个答案:

答案 0 :(得分:8)

问题的原因是通过gradle拉入的外部库,它有自己的清单请求<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18">

我自己的清单只有在maxSdkVersion =&#34; 18&#34;未包含,因此清单合并添加该参数导致此错误。我的解决方案是将我自己的清单改为:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="23" tools:replace="android:maxSdkVersion" />

我假设maxSdkVersion =&#34; 18&#34;意味着运行SDK 19-22的任何设备都没有此权限(23+能够在运行时请求它)。

答案 1 :(得分:1)

没有必要,但请尝试在您的Manifest中添加此权限。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
                    checkSelfPermission (Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED &&
                    isUriRequiresPermissions (imageUri))

有时Android会嘲笑我们。

答案 2 :(得分:0)

在Android 6中,您需要在运行时请求权限。如果你在4.4上运行,仍然有错误,我认为你还没有创建文件夹。

 String sdCardPath = Environment.getExternalStorageDirectory() + "/MyFolder";
 new File(sdCardPath) .mkdirs(); //create folders where write files
 File outFile = new File(sdCardPath, "mypicture.jpg");

答案 3 :(得分:0)

试试这个:

 String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/MyFolder");    
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-" + n + ".jpg";

File file = new File (myDir, fname);
if (file.exists ()) file.delete (); 
file.createNewFile();
try {
       FileOutputStream out = new FileOutputStream(file);
       FileInputStream in = new FileInputStream(filePath);
        byte[] buf = new byte[1024];
        int len;

        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }

        in.close();
        out.flush();
        out.close();

} catch (Exception e) {
       e.printStackTrace();
}

还为清单

添加读写权限
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />