2

I am downloading images from internet with DownloadManager and want to display them in Gallery app. I am saving the images to default 'Download' dir. Download works fine, I get notified of success. The gallery app opens, but doesn't display the image. What could be the problem?

Here's my code:

                            Cursor cursor = ((DownloadManager) getSystemService(DOWNLOAD_SERVICE)).query(ImageDownloadQuery);
                            if (cursor.moveToFirst()) {
                                String path = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
                                File file = new File(URI.create(path));
                                Uri uri = FileProvider.getUriForFile(ConversationDetailsActivity.this,
                                        BuildConfig.APPLICATION_ID + ".fileprovider",
                                        file);
                                Intent viewIntent = new Intent(Intent.ACTION_VIEW, uri);
                                viewIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                                viewIntent.setType(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_MEDIA_TYPE)));
                                if (getPackageManager().resolveActivity(viewIntent, 0) != null) { // checking if there is an app installed that can handle this type of files
                                    startActivity(viewIntent);
                                } else { // app that can view this file type is not found
                                    Toast.makeText(getBaseContext(), "Please install an application to view this type of files", Toast.LENGTH_SHORT).show();
                                }
                            }

FileProvider paths:

<paths>
    <cache-path
        name="cache"
        path=""
        />
    <external-path
        name="download"
        path="Download/"
        />
</paths>

And manifest:

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="${applicationId}.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true"
    >
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/provider_paths"
        />
</provider>

Here's a fix for those who are wondering. Remember to always set intent data and type. Setting one clears the other.

                            DownloadManager.Query ImageDownloadQuery = new DownloadManager.Query();
                            ImageDownloadQuery.setFilterById(referenceId);
                            Cursor cursor = ((DownloadManager) getSystemService(DOWNLOAD_SERVICE)).query(ImageDownloadQuery);
                            if (cursor.moveToFirst()) {
                                String path = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
                                File file = new File(URI.create(path));
                                Uri uri = FileProvider.getUriForFile(ConversationDetailsActivity.this,
                                        BuildConfig.APPLICATION_ID + ".fileprovider",
                                        file);
                                Intent viewIntent = new Intent(Intent.ACTION_VIEW);
                                viewIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                                viewIntent.setDataAndType(uri, cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_MEDIA_TYPE)));
                                if (getPackageManager().resolveActivity(viewIntent, 0) != null) { // checking if there is an app installed that can handle this type of files
                                    startActivity(viewIntent);
                                } else { // app that can view this file type is not found
                                    Toast.makeText(getBaseContext(), "Please install an application to view this type of files", Toast.LENGTH_SHORT).show();
                                }
                            }

1 Answers1

1

My bad. I set the intent type without setting data, so it cleared the Uri. Check original question for answer.