Код: Выделить всё
BroadcastReceiver onComplete = new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
Log.d(TAG, "Download commplete");
// Check for our download
long referenceId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
if (mDownloadReference == referenceId) {
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(mDownloadReference);
Cursor c = mDownloadManager.query(query);
if (c.moveToFirst()) {
int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
String localUri = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
String fileExtension = MimeTypeMap.getFileExtensionFromUrl(localUri);
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension);
if (mimeType != null) {
Intent openFileIntent = new Intent(Intent.ACTION_VIEW);
openFileIntent.setDataAndTypeAndNormalize(Uri.parse(localUri), mimeType);
openFileIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
try {
mAcme.startActivity(openFileIntent);
}
catch (ActivityNotFoundException e) {
// Ignore if no activity was found.
}
}
}
}
}
}
};
< /code>
Это работает на Android M, но ломается N из -за популярного FileUriexposedException < /code>. Теперь я пытался исправить это, используя FileProvider Код: Выделить всё
Failed to find configured root that contains /file:/storage/emulated/0/Download/test.pdflocaluri возвращается из загрузки manager для файла:
Код: Выделить всё
file:///storage/emulated/0/Download/test.pdfEnvironment.getExternalStorageDirectory () returns/storage/emulate/0 И это код для преобразования:
File file = new File(localUri);
Log.d(TAG, localUri + " - " + Environment.getExternalStorageDirectory());
Uri contentUri = FileProvider.getUriForFile(ctxt, "my.file.provider", file);
< /code>
from androidmanifest.xml < /code> < /p>
android:name="android.support.v4.content.FileProvider"
android:authorities="my.file.provider"
android:exported="false"
android:grantUriPermissions="true">
< /code>
file_paths.xml < /code>: < /p>
< /code>
И я попробовал все значения, которые я мог найти в этом файле XML. : (
Подробнее здесь: https://stackoverflow.com/questions/399 ... leprovider
Мобильная версия