J'utilise Download Manger pour télécharger des fichiers multimédia et les classer. J'utilise également Crashlytics et c'est une erreur que je reçois fréquemment sur différents appareils et versions d'Android. Je cherche vos solutions/suggestions!
Java.lang.IllegalArgumentException: Unknown URL content://downloads/my_downloads
at Android.content.ContentResolver.insert(ContentResolver.Java:862)
at Android.app.DownloadManager.enqueue(DownloadManager.Java:1252)
at com.myapp.LessonFragment$DownloadClickListener.onClick(SourceFile:570)
at Android.view.View.performClick(View.Java:4262)
at Android.view.View$PerformClick.run(View.Java:17351)
at Android.os.Handler.handleCallback(Handler.Java:615)
at Android.os.Handler.dispatchMessage(Handler.Java:92)
at Android.os.Looper.loop(Looper.Java:137)
at Android.app.ActivityThread.main(ActivityThread.Java:4921)
at Java.lang.reflect.Method.invokeNative(Method.Java)
at Java.lang.reflect.Method.invoke(Method.Java:511)
at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:1038)
at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:805)
at dalvik.system.NativeStart.main(NativeStart.Java)
Vous pouvez voir mes codes ci-dessous:
private class DownloadClickListener implements View.OnClickListener {
@Override
public void onClick(View view) {
// Check if download manager available before request
if (!DownloadHelper.isDownloadManagerAvailable(getActivity())) {
// Build custom alert dialog
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.download_manager_disabled);
builder.setCancelable(false);
builder.setPositiveButton(R.string.ok, (dialog, which) -> {
dialog.dismiss();
});
// Create and display alert dialog
AlertDialog dialog = builder.create();
dialog.show();
return;
}
// Display short toast on download clicked
Toast.makeText(getActivity(), R.string.lesson_download_start, Toast.LENGTH_SHORT).show();
// Get attach from view tag
Attache attache = (Attache) view.getTag();
// Get lesson using lesson id
Lesson lesson = new Select().from(Lesson.class)
.where(Condition.column("id").is(attache.getLessonId()))
.querySingle();
// Set file name from url and attache name
Uri uri = Uri.parse(attache.getFile());
String fileName = attache.getName() + '.'
+ MimeTypeMap.getFileExtensionFromUrl(attache.getFile());
// Check if path directory not exist and create it
String filePath = Environment.getExternalStorageDirectory() + "/myapp/" + lesson.getTitle() + "/";
File path = new File(filePath);
if (!path.exists() || !path.isDirectory()) {
if (!path.mkdirs()) {
Timber.e("Could not create path directory.");
}
}
// Check if file exist and then delete it
File file = new File(filePath, fileName);
if (file.exists() && file.isFile()) {
if (file.delete()) {
Timber.v("%s just deleted.", fileName);
}
}
// Create download manager request using url
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setTitle(attache.getName());
request.setDestinationInExternalPublicDir("/myapp/" + lesson.getTitle(), fileName);
// Using DownloadManager for download attache file
DownloadManager manager = (DownloadManager) getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
}
}
Pour ceux qui ont l'erreur Unknown URI: content://downloads/public_downloads
..__, j'ai réussi à résoudre ce problème en obtenant un indice donné par @Commonsware dans cette réponse . J'ai découvert la classe FileUtils sur GitHub. Ici, les méthodes InputStream
sont utilisées pour extraire le fichier du répertoire Download
.
// DownloadsProvider
else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
if (id != null && id.startsWith("raw:")) {
return id.substring(4);
}
String[] contentUriPrefixesToTry = new String[]{
"content://downloads/public_downloads",
"content://downloads/my_downloads",
"content://downloads/all_downloads"
};
for (String contentUriPrefix : contentUriPrefixesToTry) {
Uri contentUri = ContentUris.withAppendedId(Uri.parse(contentUriPrefix), Long.valueOf(id));
try {
String path = getDataColumn(context, contentUri, null, null);
if (path != null) {
return path;
}
} catch (Exception e) {}
}
// path could not be retrieved using ContentResolver, therefore copy file to accessible cache using streams
String fileName = getFileName(context, uri);
File cacheDir = getDocumentCacheDir(context);
File file = generateFileName(fileName, cacheDir);
String destinationPath = null;
if (file != null) {
destinationPath = file.getAbsolutePath();
saveFileFromUri(context, uri, destinationPath);
}
return destinationPath;
}
L'exception est due à Download Manager désactivé. Et il n'y a aucun moyen d'activer/désactiver Download Manager directement, car c'est l'application système et nous n'y avons pas accès.
La seule manière alternative est de rediriger l'utilisateur vers les paramètres de l'application Download Manager.
try {
//Open the specific App Info page:
Intent intent = new Intent(Android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:" + "com.Android.providers.downloads"));
startActivity(intent);
} catch ( ActivityNotFoundException e ) {
e.printStackTrace();
//Open the generic Apps page:
Intent intent = new Intent(Android.provider.Settings.ACTION_MANAGE_APPLICATIONS_SETTINGS);
startActivity(intent);
}