使用URI时,遇到了权限被拒绝,尽管FLAG_GRANT_READ_URI_PERMISSION被设置 [英] Getting permission denied when using a URI despite FLAG_GRANT_READ_URI_PERMISSION being set
问题描述
我有两个Android应用程序A和B.我是从一个应用程序传递一个文件,应用程序B和我看到该应用B被获取URI。我设置在应用程序A中的 FLAG_GRANT_READ_URI_PERMISSION
标志,我看到,那 mFlags
是一个1,这就是价值的 FLAG_GRANT_READ_URI_PERMISSION
,从应用程序内B.这是一切都很好,但是当我尝试从URI创建一个的FileInputStream
,我得到一个 FileNotFoundException异常(拒绝)
例外。我究竟做错了什么?
下面是相关的code片段:
在应用答:
公共无效openTest意图(字符串文件路径){
意图testIntent =新意图(com.example.appB.TEST);
testIntent.addCategory(Intent.CATEGORY_DEFAULT);
testIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
testIntent.setDataAndType(Uri.parse(文件://+文件路径),text / plain的);
尝试{
startActivityForResult(testIntent,OPEN_NEW_TERM);
}赶上(ActivityNotFoundException E){
// TODO自动生成catch块
e.printStackTrace();
}
}
在应用乙:
@覆盖
保护无效onNewIntent(意向意图){
super.onNewIntent(意向);
如果(intent.getAction()。等于(com.example.appB.TEST)){
乌里了fileURI = intent.getData();
文件SRCFILE =新的文件(fileUri.getPath());
文件destFolder = getFilesDir();
文件destFile =新的文件(destFolder.getAbsolutePath()+ srcFile.getName());
尝试{
copyFile(SRCFILE,destFile);
}赶上(IOException异常五){
// TODO自动生成catch块
e.printStackTrace();
}
}
}公共无效copyFile(SRC文件,文件DST)抛出IOException
在的InputStream =新的FileInputStream(SRC); // **这是它死**
出的OutputStream =新的FileOutputStream(DST); //传输的字节从到了
字节[] buf中=新的字节[1024];
INT LEN;
而((LEN = in.read(BUF))大于0){
out.write(BUF,0,LEN);
}
附寄();
out.close();
}
这是当我在创建,它获取异常。为什么有什么想法?
我是什么做错了吗?
块引用>您尝试使用
FLAG_GRANT_READ_URI_PERMISSION
的文件。这仅适用于内容://
乌里
值,指向一个服务流ContentProvider的
。使用
FileProvider
通过这样的的ContentProvider
服务您的文件。这也包括在培训指南和的这里是一个示例项目展示了其使用。I have two android apps A and B. I am passing a file from app A to app B and I am seeing that app B is getting the URI. I am setting the
FLAG_GRANT_READ_URI_PERMISSION
flag in app A and I am seeing that thatmFlags
is a 1, which is the value ofFLAG_GRANT_READ_URI_PERMISSION
, from within app B. This is all good, but when I try to create aFileInputStream
from the URI, I get aFileNotFoundException (Permission denied)
exception. What am I doing wrong?Here are the pertinent code snippets:
In app A:
public void openTest Intent(String filePath) { Intent testIntent = new Intent("com.example.appB.TEST"); testIntent.addCategory(Intent.CATEGORY_DEFAULT); testIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); testIntent.setDataAndType(Uri.parse("file://"+filePath),"text/plain"); try { startActivityForResult(testIntent, OPEN_NEW_TERM); } catch (ActivityNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
In app B:
@Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); if (intent.getAction().equals("com.example.appB.TEST")) { Uri fileUri = intent.getData(); File srcFile = new File(fileUri.getPath()); File destFolder = getFilesDir(); File destFile = new File(destFolder.getAbsolutePath()+srcFile.getName()); try { copyFile(srcFile,destFile); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public void copyFile(File src, File dst) throws IOException { InputStream in = new FileInputStream(src); //**this is where it dies** OutputStream out = new FileOutputStream(dst); // Transfer bytes from in to out byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close(); out.close(); }
It is when I am creating
in
that it gets the exception. Any thoughts on why?解决方案What am I doing wrong?
You are trying to use
FLAG_GRANT_READ_URI_PERMISSION
for a file. That only works forcontent://
Uri
values, pointing to a stream served by aContentProvider
.Use
FileProvider
to serve up your files via such aContentProvider
. This is also covered in a training guide, and here is a sample project demonstrating its use.这篇关于使用URI时,遇到了权限被拒绝,尽管FLAG_GRANT_READ_URI_PERMISSION被设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!