Reputation: 577
I've opened pdf file from different sources and got this Intents
:
File manager (Google Files) -> Internal storage -> Download: 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 }
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 }
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 }
Telegram chat: 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 }
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?
If there is a misunderstanding with cases 1 and 2, take a look to screenshots in this question
Upvotes: 3
Views: 19463
Reputation: 56
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
PACKAGE_NAME
depends on application.CONTENT_DETAIL
is optional, either file_id or full_path.If you decompile all these four applications and look at their AndroidManifest.xml
you will see the same provider tag with minor differences.
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>
This provider will generate the HierarchicalUri
's like bellow
content://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.
Upvotes: 0