我已经从不同的来源打开了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有误解,请查看this question中的屏幕截图
发布于 2021-06-28 23:48:57
这四个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
content://PACKAGE_NAME
.provider/CONTENT_DETAIL
显然,application.
CONTENT_DETAIL
是可选的,无论是file_id还是full_path.。如果您对这四个应用程序进行反编译,并查看它们的AndroidManifest.xml
,您将看到相同的提供者标记,但差别很小。
看看Google的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();
}
}
我希望我已经回答了你的问题。
https://stackoverflow.com/questions/67638455
复制相似问题