Я загружаю изображения из Интернета с помощью DownloadManager и хочу отображать их в приложении Галерея. Я сохраняю изображения по умолчанию 'Download'. Скачать работает отлично, я получаю уведомление об успехе. Приложение галереи открывается, но не отображает изображение. В чем может быть проблема?Android посмотреть изображение, загруженное с DownloadManager
Вот мой код:
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>
<cache-path
name="cache"
path=""
/>
<external-path
name="download"
path="Download/"
/>
</paths>
И манифеста:
<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>
Вот исправление для тех, кто интересно. Не забудьте всегда задавать данные и тип умывания. Установка одного очищает другой.
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();
}
}