问题描述
我快疯了,过去我曾经使用过新的Android FileProvider
,但是我无法将其与Download文件夹中的一个(琐碎的)刚刚创建的文件一起使用.
I'm going crazy, I used the new Android FileProvider
in the past but I can't get it to work with a (trivial) just-created file in the Download folder.
在我的AsyncTask.onPostExecute
我打电话
Intent myIntent = new Intent(Intent.ACTION_VIEW, FileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID + ".fileprovider", output));
myIntent.setType("text/plain");
myIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(myIntent);
我的FileProvider XML就是这样
My FileProvider XML is like this
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="Download" path="Download"/>
</paths>
在Genymotion模拟器中,我总是选择Amaze Text Editor作为目标应用程序:
In Genymotion emulator I always get, choosing Amaze Text Editor as target app:
虽然我可以使用HTML Viewer查看文件内容:
While I can see the file content with HTML Viewer:
我无法理解这种行为,无法解决琐碎的事情,例如使用所需的文本编辑器打开纯文本文件.
I can't understand this behavior and fix what should be a trivial thing like opening a pure-text file with the desidered text editor.
非常感谢尼古拉
推荐答案
好的,这里有两个问题.一种是您的代码中的错误会触发Amaze中的错误,另一种是您可以解决的Amaze中的错误.
OK, there are two problems here. One is a bug in your code that triggers a bug in Amaze, and one is a bug in Amaze that you can work around.
setType()
有一个令人讨厌的副作用:它会擦掉Intent
中的Uri
.这等效于调用setDataAndType(null, ...)
(其中...
是您的MIME类型).这不好.因此,不要将Uri
放入构造函数中并调用setType()
,而是要调用setDataAndType()
并在其中提供Uri
.这使您摆脱了最初的Amaze错误,在该错误中他们无法正确处理null
Uri
.
setType()
has a nasty side effect: it wipes out your Uri
in the Intent
. It is the equivalent of calling setDataAndType(null, ...)
(where ...
is your MIME type). That's not good. So, instead of putting the Uri
in the constructor and calling setType()
, call setDataAndType()
and provide the Uri
there.This gets you past the initial Amaze bug, where they fail to handle a null
Uri
correctly.
然后,他们尝试以读写模式打开Uri
.您仅授予读取访问权限,因此失败.他们的第二个错误是,当他们无法以读写模式打开文件时,他们以为得到FileNotFoundException
,这时他们尝试了只读模式.实际上,至少在Android 8.1上,它们会得到SecurityException
.您可以通过提供读写权限来解决此问题.
Then, though, they try to open the Uri
in read-write mode. You are only granting read access, so this fails. Their second bug is that they think that they get a FileNotFoundException
when they cannot open the file in read-write mode, and at that point they try read-only mode. In reality, at least on Android 8.1, they get a SecurityException
. You can work around this by providing both read and write permissions.
因此,除非您特别希望阻止写访问,否则此代码有效:
So, unless you specifically want to block write access, this code works:
Intent myIntent = new Intent(Intent.ACTION_VIEW);
myIntent.setDataAndType(FileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID + ".fileprovider", output), "text/plain");
myIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION|Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
startActivity(myIntent);
这篇关于无法使用FileProvider打开平凡的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!