android– 未知的URL内容: downloadsmy_downloads

android– 未知的URL内容: downloadsmy_downloads,第1张

概述我正在使用DownloadManger下载一些多媒体文件并对其进行分类.我也在使用Crashlytics,这是一个错误,我经常在不同的设备和Android版本上得到它.我正在寻找你的解决方案/建议!java.lang.IllegalArgumentException:UnknownURLcontent://downloads/my_downloadsatandroid.con

我正在使用Download manger下载一些多媒体文件并对其进行分类.我也在使用Crashlytics,这是一个错误,我经常在不同的设备和Android版本上得到它.我正在寻找你的解决方案/建议!

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)

您可以在下面看到我的代码:

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);    }}

解决方法:

对于那些获得错误未知URI的人:content:// downloads / public_downloads.
我设法通过@Commonsware在this answer中给出一个提示来解决这个问题.我在GitHub上找到了类FileUtils.
这里的inputStream方法用于从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;            }
总结

以上是内存溢出为你收集整理的android – 未知的URL内容:// downloads / my_downloads全部内容,希望文章能够帮你解决android – 未知的URL内容:// downloads / my_downloads所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1100641.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存