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

Android 尝试在外部存储器上打开文件时权限被拒绝,android,file,permissions,Android,File,Permissions,我已收到一个内容uri,该uri指向作为活动结果接收的图像* 我只是试图将该文件复制到另一个文件中,但当我试图打开目标文件时,权限一直被拒绝。有人知道我做错了什么吗 Uri u = Uri.parse( data.getDataString()); ContentResolver contentResolver = getContentResolver(); try { InputStream is = contentResolver.openInput

我已收到一个内容uri,该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);
外部写入权限在清单中声明,并且已授予运行时权限。

*这里是使用的意图

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)
许可被授予:

我遇到了相同的问题,可以通过以下任一步骤轻松解决: 1.如果安装在Android N和O版本上,请使用-g安装您的应用程序。 2.手动授予权限 设置->应用->应用名称->权限->启用切换

对于步骤1和2,define使用权限android:name=“android.permission.WRITE\u EXTERNAL\u STORAGE” 并使用权限android:name=“android.permission.READ\u EXTERNAL\u STORAGE”
AndroidManifest.xml中的属性

什么API版本?查看在运行时权限检查的任何版本上,您是否需要向用户请求权限或从“设置”应用程序授予权限。请参阅已授予@Gabe运行时权限。SDK版本25@MuraliPrajapati运行时权限已授予,但我仍然收到此错误。请尝试运行较低版本的android设备,以检查问题是否与权限或其他任何内容有关。