【发布时间】:2017-04-06 20:42:01
【问题描述】:
我收到了作为活动结果接收到的图像的内容 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);
在manifest中声明了外部写入权限,并已授予运行时权限。<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)
【问题讨论】:
-
什么 API 版本?如果在任何具有运行时权限检查的版本上,请参阅 stackoverflow.com/questions/33162152/…
-
您需要询问用户或从设置应用授予权限。请参考developer.android.com/training/permissions/requesting.html
-
@Gabe Runtime 权限已被授予。 SDK 版本 25
-
@MuraliPrajapati 运行时权限已被授予,但我仍然收到此错误。
-
尝试运行低安卓版本的设备,检查问题是否与权限或其他问题有关。
标签: android file permissions