我打开了来自不同来源的pdf文件,得到了这个Intents
:
-
文件管理器(谷歌文件(->内部存储->下载:
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 }
-
文件管理器(谷歌文件(->下载:
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 }
-
电报聊天:
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不能?
如果情况1和2存在误解,请查看此问题中的屏幕截图
所有这四个Uri都遵循相同的模式
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
内容://PACKAGE_NAME
.provider/CONTENT_DETAIL
PACKAGE_NAME
取决于应用程序
CONTENT_DETAIL
是可选的,可以是file_id,也可以是full_path如果您反编译所有这四个应用程序并查看它们的AndroidManifest.xml
,您将看到相同的provider标记,但略有不同。
查看Google Files的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
使用下面的代码段,您可以从这四个应用程序中读取文件/内容。
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();
}
}
我希望我已经回答了你的问题。