Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 未知URLcontent://downloads/my_downloads_Android_Download_Android Download Manager - Fatal编程技术网

Android 未知URLcontent://downloads/my_downloads

Android 未知URLcontent://downloads/my_downloads,android,download,android-download-manager,Android,Download,Android Download Manager,我正在使用下载管理器下载一些多媒体文件并对它们进行分类。我也在使用Crashlytics,这是一个错误,我经常在不同的设备和Android版本上看到它。我正在寻找你的解决方案/建议 java.lang.IllegalArgumentException: Unknown URL content://downloads/my_downloads at android.content.ContentResolver.insert(ContentResolver.java:862) at a

我正在使用下载管理器下载一些多媒体文件并对它们进行分类。我也在使用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)
您可以在下面看到我的代码:

私有类下载ClickListener实现View.OnClickListener{
@凌驾
公共void onClick(视图){
//在请求之前检查下载管理器是否可用
如果(!DownloadHelper.isDownloadManagerAvailable(getActivity())){
//生成自定义警报对话框
AlertDialog.Builder=新建AlertDialog.Builder(getActivity());
builder.setMessage(R.string.download\u manager\u禁用);
builder.setCancelable(false);
setPositiveButton(R.string.ok,(对话框,其中)->{
dialog.dismise();
});
//创建和显示警报对话框
AlertDialog=builder.create();
dialog.show();
返回;
}
//单击下载时显示简短的toast
Toast.makeText(getActivity(),R.string.lesson_下载_开始,Toast.LENGTH_SHORT).show();
//从视图标记获取附加
Attache Attache=(Attache)view.getTag();
//使用课程id获取课程
Lesson Lesson=新建选择().from(Lesson.class)
其中(Condition.column(“id”)是(attache.getLessonId())
.querySingle();
//根据url和附件名设置文件名
Uri=Uri.parse(attache.getFile());
字符串文件名=attache.getName()+'
+MimeTypeMap.getFileExtensionFromUrl(attache.getFile());
//检查路径目录是否不存在并创建它
字符串filePath=Environment.getExternalStorageDirectory()+“/myapp/”+lesson.getTitle()+“/”;
文件路径=新文件(文件路径);
如果(!path.exists()| |!path.isDirectory()){
如果(!path.mkdirs()){
e(“无法创建路径目录”);
}
}
//检查文件是否存在,然后将其删除
文件=新文件(文件路径、文件名);
if(file.exists()&&file.isFile()){
if(file.delete()){
Timber.v(“%s刚刚删除”,文件名);
}
}
//使用url创建下载管理器请求
DownloadManager.Request=新的DownloadManager.Request(uri);
request.setTitle(attache.getName());
request.setDestinationNexternalPublicDir(“/myapp/”+lesson.getTitle(),文件名);
//使用DownloadManager下载附件文件
DownloadManager=(DownloadManager)getActivity().getSystemService(Context.DOWNLOAD_服务);
排队(请求);
}
}

异常是由禁用的下载管理器引起的。而且没有办法直接激活/停用下载管理器,因为它是系统应用程序,我们无权访问它

唯一的替代方法是将用户重定向到下载管理器应用程序的设置

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

对于获得错误
未知URI的用户:content://downloads/public_downloads
。 我通过在中得到@commonware给出的提示来解决这个问题。我在GitHub上发现了这个类。 这里使用
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;
            }

我遇到了异常java.lang.IllegalArgumentException:Unknown URI:content://downloads/public_downloads/7505 从下载中获取文件。这个解决方案对我有效。

else if (isDownloadsDocument(uri)) {
            String fileName = getFilePath(context, uri);
            if (fileName != null) {
                return Environment.getExternalStorageDirectory().toString() + "/Download/" + fileName;
            }

            String id = DocumentsContract.getDocumentId(uri);
            if (id.startsWith("raw:")) {
                id = id.replaceFirst("raw:", "");
                File file = new File(id);
                if (file.exists())
                    return id;
            }

            final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
            return getDataColumn(context, contentUri, null, null);
        }
这是用于获取文件路径的方法

   public static String getFilePath(Context context, Uri uri) {

    Cursor cursor = null;
    final String[] projection = {
            MediaStore.MediaColumns.DISPLAY_NAME
    };

    try {
        cursor = context.getContentResolver().query(uri, projection, null, null,
                null);
        if (cursor != null && cursor.moveToFirst()) {
            final int index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DISPLAY_NAME);
            return cursor.getString(index);
        }
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return null;
}

我也遇到了同样的问题,在花了很多时间搜索之后,我找到了解决方案

只需更改您的方法,尤其是//下载Provider部分

getpath()

@SuppressLint(“新API”) 公共静态字符串getPath(最终上下文,最终Uri){

有关更多解决方案,请单击此处的链接


我希望我也能为你做同样的事!

我猜
attache.getFile()
返回
content://downloads/my_downloads
?在这种情况下,您可能存储了错误的值。您提供给downloadmanager的链接是一个本地URI,用于标识从Androids downloadmanager下载的内容。谢谢,但实际上您的猜测是错误的。
attache.getFile()
返回服务器上文件的url。手机中的“DownloadManager”进程有问题,最可能的问题是手机中的“DownloadManager”被禁用。请参阅“设置->应用程序->全部->DownloadManager”,如果禁用则启用。重复:[请检查此链接。此问题的解决方案非常好]()为了补充这个答案,我提到两个contenturi前缀在我的例子中是不够的,必须添加
”content://downloads/all_downloads"
也是。这取决于不同的手机。我们可以添加您的
content://downloads/all_downloads
这段代码也是如此。如果这些不起作用,那么我们使用InputStream函数。这不起作用,我用你在链接中提到的文件utils替换了我的文件utils。然后,我用你上面提供的内容替换了DowloadsProvider部分。When我从我的SD卡中选择了一个视频/mp4,Uri为空。获取异常或无异常为空?此外,我还没有使用可移动SD卡进行测试。@BipinBharti这里是对您问题的答案快乐编码。我在获取Android 9的文件名时遇到问题->content://com.android.providers.downloads.documents ->*,但其他内容hways提供正确的文件名。此解决方案帮助我获得正确的文件名。谢谢!
    final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;

    // DocumentProvider
    if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
        // ExternalStorageProvider
        if (isExternalStorageDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            // This is for checking Main Memory
            if ("primary".equalsIgnoreCase(type)) {
                if (split.length > 1) {
                    return Environment.getExternalStorageDirectory() + "/" + split[1];
                } else {
                    return Environment.getExternalStorageDirectory() + "/";
                }
                // This is for checking SD Card
            } else {
                return "storage" + "/" + docId.replace(":", "/");
            }

        }
        // DownloadsProvider
        else if (isDownloadsDocument(uri)) {
            String fileName = getFilePath(context, uri);
            if (fileName != null) {
                return Environment.getExternalStorageDirectory().toString() + "/Download/" + fileName;
            }

            String id = DocumentsContract.getDocumentId(uri);
            if (id.startsWith("raw:")) {
                id = id.replaceFirst("raw:", "");
                File file = new File(id);
                if (file.exists())
                    return id;
            }

            final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
            return getDataColumn(context, contentUri, null, null);
        }
        // MediaProvider
        else if (isMediaDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            Uri contentUri = null;
            if ("image".equals(type)) {
                contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            } else if ("video".equals(type)) {
                contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
            } else if ("audio".equals(type)) {
                contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            }

            final String selection = "_id=?";
            final String[] selectionArgs = new String[]{
                    split[1]
            };

            return getDataColumn(context, contentUri, selection, selectionArgs);
        }
    }
    // MediaStore (and general)
    else if ("content".equalsIgnoreCase(uri.getScheme())) {

        // Return the remote address
        if (isGooglePhotosUri(uri))
            return uri.getLastPathSegment();

        return getDataColumn(context, uri, null, null);
    }
    // File
    else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}