尝试在外部存储上打开文件时权限被拒绝

时间:2017-04-06 20:42:01

标签: android file permissions

我收到了因活动*

而收到的图片的内容uri

我只是想将该文件复制到另一个文件,但是当我尝试打开目标文件时,我一直拒绝获得权限。谁知道我做错了什么?

    Uri u = Uri.parse( data.getDataString());
    ContentResolver contentResolver = getContentResolver();

    try {
        InputStream is = contentResolver.openInputStream(u);

        if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            // media is not available to use. Abort.
            Toast.makeText(this, "Storage Unavailable.", Toast.LENGTH_LONG).show();
            return;
        }

        String dir = Environment.getExternalStorageDirectory().toString();
        File f = new File(dir, "test.jpg");
        f.createNewFile();
        // exception thrown
        // java.io.FileNotFoundException: /storage/emulated/0/test.jpg (Permission denied)
        FileOutputStream fose = new FileOutputStream(f); 

        byte[] b = new byte[1024];

        while (is.read(b) != -1) {
            fileOutputStream.write(b);
            fose.write(b);
        }
        fileOutputStream.close();
        is.close();
    } catch (IOException e) {
        Toast.makeText(this,e.getMessage(),Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }
    ImageView iv = (ImageView) findViewById(R.id.theimage);
    iv.setImageURI(u);

外部写入权限在清单中声明,并且已授予运行时权限 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

*这里是使用的意图

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
   startActivityForResult(Intent.createChooser(intent, "Select a File"), FILE_SELECT_CODE);
} catch (android.content.ActivityNotFoundException ex) {
   Toast.makeText(this, "Please install a File Manager.", Toast.LENGTH_SHORT).show();
}

编辑:完整堆栈跟踪:

6 15:23:27.068 6181-6181/test.example.x W/System.err: java.io.IOException: Permission denied
04-06 15:23:27.068 6181-6181/test.example.x W/System.err:     at java.io.UnixFileSystem.createFileExclusively0(Native Method)
04-06 15:23:27.068 6181-6181/test.example.x W/System.err:     at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:280)
04-06 15:23:27.068 6181-6181/test.example.x W/System.err:     at java.io.File.createNewFile(File.java:948)
04-06 15:23:27.068 6181-6181/test.example.x W/System.err:     at test.example.x.ui.MainActivity.onActivityResult(MainActivity.java:173)
04-06 15:23:27.068 6181-6181/test.example.x W/System.err:     at android.app.Activity.dispatchActivityResult(Activity.java:6915)
04-06 15:23:27.068 6181-6181/test.example.x W/System.err:     at android.app.ActivityThread.deliverResults(ActivityThread.java:4049)
04-06 15:23:27.068 6181-6181/test.example.x W/System.err:     at android.app.ActivityThread.handleSendResult(ActivityThread.java:4096)
04-06 15:23:27.069 6181-6181/test.example.x W/System.err:     at android.app.ActivityThread.-wrap20(ActivityThread.java)
04-06 15:23:27.069 6181-6181/test.example.x W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1516)
04-06 15:23:27.069 6181-6181/test.example.x W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
04-06 15:23:27.069 6181-6181/test.example.x W/System.err:     at android.os.Looper.loop(Looper.java:154)
04-06 15:23:27.069 6181-6181/test.example.x W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6077)
04-06 15:23:27.069 6181-6181/test.example.x W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
04-06 15:23:27.069 6181-6181/test.example.x W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
04-06 15:23:27.069 6181-6181/test.example.x W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

授予 的权限: http://i.imgur.com/AeWcmYG.png

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,可以通过以下任一步骤轻松删除它: 1.如果在Android N和O版本上安装,请使用-g安装您的应用程序。 2.手动授予权限  设置 - &gt; apps-&gt;&#34; app_name&#34; - &gt;权限 - &gt;启用开关

对于步骤1和2,定义uses-permission android:name =&#34; android.permission.WRITE_EXTERNAL_STORAGE&#34; 和uses-permission android:name =&#34; android.permission.READ_EXTERNAL_STORAGE&#34; AndroidManifest.xml中的属性