问题描述
我打开了来自不同来源的 pdf 文件并得到了这个 Intents
:
I've opened pdf file from different sources and got this Intents
:
文件管理器(Google 文件)->内部存储 ->下载:
Intent { act=android.intent.action.VIEW dat=content://com.google.android.apps.nbu.files.provider/1/file:///storage/emulated/0/Download/Untitled.pdf typ=application/pdf flg=0x13000001 cmp=team.sls.testapp/.ActivityMain }
文件管理器(Google 文件)->下载:Intent { act=android.intent.action.VIEW dat=content://com.google.android.apps.nbu.files.provider/2/1863 typ=application/pdf flg=0x13000001 cmp=team.sls.testapp/.ActivityMain }
File manager (Google Files) -> Downloads: Intent { act=android.intent.action.VIEW dat=content://com.google.android.apps.nbu.files.provider/2/1863 typ=application/pdf flg=0x13000001 cmp=team.sls.testapp/.ActivityMain }
下载管理器(来自通知面板):Intent { act=android.intent.action.VIEW dat=content://com.android.providers.downloads.documents/document/1508 typ=application/pdf flg=0x13000003 cmp=team.sls.testapp/.ActivityMain }
Dowdload manager (from notification panel): Intent { act=android.intent.action.VIEW dat=content://com.android.providers.downloads.documents/document/1508 typ=application/pdf flg=0x13000003 cmp=team.sls.testapp/.ActivityMain }
电报聊天:Intent { act=android.intent.action.VIEW dat=content://org.telegram.messenger.provider/media/Telegram/Telegram Documents/2_5276292126848585208.pdf typ=application/pdf flg=0x13000001 cmp=team.sls.testapp/.ActivityMain }
什么是 content://com.google.android.apps.nbu.files.provider/2/1863
,为什么同一文件的路径不同?但更有趣的是 - 为什么情况 1 和 4 可以打开带有自定义扩展名的文件,而情况 2 和 3 却不能?
What is content://com.google.android.apps.nbu.files.provider/2/1863
and why the path to same file is different?But more interesting - why cases 1 and 4 can open files with custom extensions but 2 and 3 don't?
如果对案例 1 和案例 2 有误解,请查看这个问题中的截图
If there is a misunderstanding with cases 1 and 2, take a look to screenshots in this question
推荐答案
所有这四个 Uri 都遵循相同的模式
All these four Uri's are following the same pattern
content://com.google.android.apps.nbu.files.provider/1/file:///storage/emulated/0/Download/Untitled.pdf
content://com.google.android.apps.nbu.files.provider/2/1863
content://com.android.providers.downloads.documents/document/1508
content://org.telegram.messenger.provider/media/Telegram/Telegram Documents/2_5276292126848585208.pdf
content://PACKAGE_NAME
.provider/CONTENT_DETAIL
content://PACKAGE_NAME
.provider/CONTENT_DETAIL
- 显然,
PACKAGE_NAME
取决于应用程序. CONTENT_DETAIL
是可选的,可以是 file_id 或 full_path.
- Obviously the
PACKAGE_NAME
depends on application. - And the
CONTENT_DETAIL
is optional, either file_id or full_path.
如果您反编译所有这四个应用程序并查看它们的 AndroidManifest.xml
,您将看到相同的 provider 标记,但有细微差别.
If you decompile all these four applications and look at their AndroidManifest.xml
you will see the same provider tag with minor differences.
查看 Google Files 的 AndroidManifest.xml
Look at Google Files's AndroidManifest.xml
<application>
<provider
android:name="com.google.android.libraries.storage.storagelib.FileProvider"
android:exported="false"
android:authorities="com.google.android.apps.nbu.files.provider"
android:grantUriPermissions="true"/>
</application>
此提供程序将生成如下所示的 HierarchicalUri
content://com.google.android.apps.nbu.files.provider/1651/2
This provider will generate the HierarchicalUri
's like bellowcontent://com.google.android.apps.nbu.files.provider/1651/2
使用下面的代码段,您可以从所有这四个应用程序中读取文件/内容.
Using the snippet bellow, you can read file/content from all those four application's.
public final class MainActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
InputStream is = getContentResolver().openInputStream(intent.getData());
String content = new String(Utils.unwrap(is));
Log.i("TAG", "content: " + content);
}
public static byte[] unwrap(InputStream is) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[4096];
while ((nRead = is.read(data, 0, data.length)) != -1) {
baos.write(data, 0, nRead);
}
baos.flush();
is.close();
baos.close();
return baos.toByteArray();
}
}
我希望我已经回答了你的问题.
I hope i have answered your question.
这篇关于什么是 com.google.android.apps.nbu.files.provider/2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!