jeudi 21 mai 2015

Cannot open a PDF or an image with Intent(Intent.ACTION_VIEW) and URI retrieved from DownloadManager's request in Android 4.4

I want to open a PDF file or an image with an intent from a URI which I retrieved from a DownloadManager's request. It works just fine on Android 5.0, but for whatever reason it does not work on 4.4 and below. Here is my code.

mDownloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);    
DownloadManager.Request request = new DownloadManager.Request(
                        Uri.parse(mDownloadableUrl));
mEnqueue = mDownloadManager.enqueue(request);


mBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
                    DownloadManager.Query query = new DownloadManager.Query();
                    query.setFilterById(mEnqueue);
                    Cursor c = mDownloadManager.query(query);
                    if (c.moveToFirst()) {
                        int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
                        if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
                            String uriString = c.getString(
                                    c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
                            LogUtils.LOGD(TAG, "downloaded a file in " + uriString);
                            mDocumentUri = Uri.parse(uriString);
                            Intent intent = new Intent(Intent.ACTION_VIEW);
                                intent.setData(mDocumentUri);
                                startActivity(intent);
                            }
                        }
                    }
                    c.close();
                }
            }
        };

 registerReceiver(mBroadcastReceiver,
            new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

the core part really is the following where I initiate startActivity(intent) with an URI that is retrieved as shown above. Uri looks like content://downloads/my_downloads/774

It correctly downloads a file in Downloads which I can see and open a file from Downloads app. But when I try to open an Activity from the intent, the opened app shows an error that it cannot open the file.

The log below is one of the PDF applications that I tried to open the file but failed

D/StatusBarManagerService(  716): tr p:27706,o:f
W/System.err(27706): java.io.FileNotFoundException: No entry for content://downloads/my_downloads/773
W/System.err(27706):    at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:146)
W/System.err(27706):    at android.content.ContentProviderProxy.openTypedAssetFile(ContentProviderNative.java:682)
W/System.err(27706):    at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1066)
W/System.err(27706):    at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:907)
W/System.err(27706):    at android.content.ContentResolver.openInputStream(ContentResolver.java:632)
W/System.err(27706):    at com.infraware.polarisoffice5.OfficeLauncherActivity.getOpenFilePath(OfficeLauncherActivity.java:341)
W/System.err(27706):    at com.infraware.polarisoffice5.OfficeLauncherActivity.onOpenViewer(OfficeLauncherActivity.java:188)
W/System.err(27706):    at com.infraware.polarisoffice5.OfficeLauncherActivity.onCreate(OfficeLauncherActivity.java:125)
W/System.err(27706):    at android.app.Activity.performCreate(Activity.java:5451)
W/System.err(27706):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
W/System.err(27706):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2359)
W/System.err(27706):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2453)
W/System.err(27706):    at android.app.ActivityThread.access$900(ActivityThread.java:173)
W/System.err(27706):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
W/System.err(27706):    at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err(27706):    at android.os.Looper.loop(Looper.java:136)
W/System.err(27706):    at android.app.ActivityThread.main(ActivityThread.java:5579)
W/System.err(27706):    at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err(27706):    at java.lang.reflect.Method.invoke(Method.java:515)
W/System.err(27706):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
W/System.err(27706):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
W/System.err(27706):    at dalvik.system.NativeStart.main(Native Method)

I have android.permission.INTERNET permission in my manifest. The exact same code works just fine on Android 5.0; but when I run this on 4.4 and try to open a PDF file or image file with URI intent, then it fails.

This problem still occurs when I add WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE permissions. Intent.setDataAndType(android.net.Uri, java.lang.String) with "application/pdf" did not help. I tried with intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); that didn't help either. I am running out of ideas.

Do you see any issues?

Aucun commentaire:

Enregistrer un commentaire