Unable to query ContentResolver to get downloaded file's name from my_downloads but works fine in all_downloads

扶醉桌前 提交于 2019-12-04 12:17:21

问题


My app is set up to activate once a file with extension .myappdata is opened. It accomplishes this through this intent-filter:

    <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 a Chrome web browser (on a Nexus 7 running Android 6.0.1) and go to a website that downloads a file named "file123.myappdata" and click the Open link within Chrome.

That action triggers my app to open and this code to run, which will get the filename that was downloaded:

    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 runs (after I open the file from Chrome) the uri is content://downloads/my_downloads/1602 and cursor.moveToFirst() returns false

Then I exit my app and go to the Downloads app and open the exact same file. That triggers my app to open, the same code to run except with uri content://downloads/all_downloads/1602 (everything else in the uri is exactly the same), the cursor.moveToFirst() call returns true and the cursor.getString() method returns "file123.myappdata" (which is exactly what I want).

Why is the cursor.moveToFirst() method not finding any data when I open the downloaded file from Chrome (giving me "my_downloads" in the uri) versus opening the same downloaded file from Downloads (giving me "all_downloads" in the uri)?

来源:https://stackoverflow.com/questions/36483196/unable-to-query-contentresolver-to-get-downloaded-files-name-from-my-downloads

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!