Failed to request ContentResolver to get loaded file name from my_downloads, but works fine in all_downloads

My application is configured to activate after opening a file with the extension .myappdata. He does this through this filter intent:

<activity android:name="com.myapp.ExamineFileActivity"> <intent-filter> <action android:name="android.intent.action.VIEW"/> <category android:name="android.intent.category.BROWSABLE"/> <category android:name="android.intent.category.DEFAULT"/> <data android:scheme="file" android:pathPattern=".*\\.myappdata" android:mimeType="*/*"/> <data android:scheme="content" android:pathPattern=".*\\.myappdata" android:mimeType="*/*"/> </intent-filter> </activity> 

I open the Chrome browser (on Nexus 7 running Android 6.0.1) and go to the website downloading the file called "file123.myappdata" and click the "Open" link in Chrome.

This action launches my application to open and runs this code that will load the downloaded file name:

  Uri uri = this.getIntent().getData(); if (uri.getScheme().equals("content")) { Cursor cursor = getContentResolver().query(uri, null, null, null, null); try { if (cursor != null && cursor.moveToFirst()) { return cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)); } } finally { cursor.close(); } } 

when this code is run (after I open the file from Chrome) uri is content: // downloads / my_downloads / 1602 and cursor.moveToFirst () returns false

Then I exit my application and go to the Downloads application and open the same file. This launches my application to open, the same code runs, except for the contents of uri: // downloads / all_downloads / 1602 (everything else in the uri is exactly the same), calling cursor.moveToFirst () returns true , and the cursor.getString () method returns "file123.myappdata" (which is exactly what I want).

Why does the cursor.moveToFirst () method not find any data when I open the downloaded file from Chrome (giving me "my_downloads" in the uri) compared to opening the same downloaded file from the downloads (giving me "all_downloads" in the URI)?

+8
source share
1 answer

I had the same problem

use below code for

 if (isDownloadsDocument(uri)) { String fileName = getFilePath(context, uri); if (fileName != null) { return Environment.getExternalStorageDirectory().toString() + "/Download/" + fileName; } final String id = DocumentsContract.getDocumentId(uri); final Uri contentUri = ContentUris.withAppendedId( Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)); return getDataColumn(context, contentUri, null, null); } 

And getFilePath ()

 public static String getFilePath(Context context, Uri uri) { Cursor cursor = null; final String[] projection = { MediaStore.MediaColumns.DISPLAY_NAME }; try { cursor = context.getContentResolver().query(uri, projection, null, null, null); if (cursor != null && cursor.moveToFirst()) { final int index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DISPLAY_NAME); return cursor.getString(index); } } finally { if (cursor != null) cursor.close(); } return null; } 

link: this link resolved

-1
source

Source: https://habr.com/ru/post/1246646/


All Articles