无法处理uri :: content:// media / external / images / media / 221686 DownloadManager.Request

时间:2019-02-17 05:26:43

标签: java android android-download-manager

当我尝试将图像保存到InternalStorage

我收到此错误

  

java.lang.IllegalArgumentException:无法处理uri ::   内容:// media / external / images / media / 221686           在android.app.DownloadManager $ Request.checkUri(DownloadManager.java:709)           在android.app.DownloadManager $ Request。(DownloadManager.java:693)

我的代码

String name = getIntent().getStringExtra("name");
            String filename = name+" MyImages.jpg";
            //  String downloadUrlOfImage = Uri.fromFile(b);
            Bitmap bitmap = getBitmapFromView(view);
            File direct =
                    new File(Environment
                            .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
                            .getAbsolutePath() + "/" + "MyImages" + "/");


            if (!direct.exists()) {
                direct.mkdir();
                Log.d(TAG, "dir created for first time");
            }
            DownloadManager dm = (DownloadManager) getApplicationContext().getSystemService(Context.DOWNLOAD_SERVICE);
            DownloadManager.Query query = new DownloadManager.Query();
            Uri downloadUri = getImageUri(this, bitmap);
            DownloadManager.Request request = new DownloadManager.Request(downloadUri);
            request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
                    .setAllowedOverRoaming(false)
                    .setTitle(filename)
                    .setMimeType("image/jpeg")
                    .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
                    .setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES,
                            File.separator + "MyImages" + File.separator + filename);
            Cursor cursor = dm.query(query);
            if (cursor.moveToFirst()) {
                if (cursor.getCount() > 0) {
                    int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
                    if (status == DownloadManager.STATUS_SUCCESSFUL)
                    {
                        Toast.makeText(getApplicationContext(), "Image Successfully Downloaded", Toast.LENGTH_SHORT).show();
                    }
                }
            }
            cursor.close();
            dm.enqueue(request);

0 个答案:

没有答案