Java 从android URL下载imageview中加载的图像

Java 从android URL下载imageview中加载的图像,java,android,Java,Android,我在URL的image视图中加载了一个image。我想下载图像并将其保存到外部存储器 这里我使用了Glide从URL加载image Glide.with( this ) .load( url ) .placeholder(R.drawable.placeholder) // any placeholder to load at start .error(R.drawable.imagenotfound

我在
URL
image视图中加载了一个
image
。我想下载图像并将其保存到外部存储器

这里我使用了
Glide
URL
加载
image

Glide.with( this )
                .load( url )
                .placeholder(R.drawable.placeholder) // any placeholder to load at start
                .error(R.drawable.imagenotfound)  // any image in case of error

                .diskCacheStrategy( DiskCacheStrategy.ALL )
                .into(meme_Image_view);
接下来,当用户单击下载按钮时,我想将
图像视图
中加载的
图像
下载到外部存储器

下面是我使用的代码:

download_btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                if(checkPermission(
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        STORAGE_PERMISSION_CODE)){



                    BitmapDrawable draw = (BitmapDrawable) meme_Image_view.getDrawable();
                    Bitmap bitmap = draw.getBitmap();

                    FileOutputStream outStream = null;
                    File sdCard = Environment.getExternalStorageDirectory();
                    File dir = new File(sdCard.getAbsolutePath() + "/TirunelveliParithabangal");
                    dir.mkdirs();
                    String fileName = String.format("%d.jpg", System.currentTimeMillis());
                    File outFile = new File(dir, fileName);
                    try {
                        outStream = new FileOutputStream(outFile);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
                    try {
                        outStream.flush();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    try {
                        outStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    TastyToast.makeText(getApplicationContext(), "Meme Downloaded Successfully", TastyToast.LENGTH_LONG*6, TastyToast.SUCCESS);
                }
                else {
                    TastyToast.makeText(getApplicationContext(), "Please Grant Permission", TastyToast.LENGTH_LONG*6, TastyToast.SUCCESS);
                }
            }
        });
此处显示权限检查功能:

 // Function to check and request permission.
    public boolean checkPermission(final String permission, final int requestCode)
    {
        if (ContextCompat.checkSelfPermission(ImageViewActivity.this, permission)
                == PackageManager.PERMISSION_DENIED) {

            Pop.on(this).with().layout(R.layout.permission_alert_info).when(new Pop.Yah() {
                @Override
                public void clicked(DialogInterface dialog, View view) {
                    // Requesting the permission
                    ActivityCompat.requestPermissions(ImageViewActivity.this,
                            new String[] { permission },
                            requestCode);
                }
            }).show();
        }
        else {
            return true;
        }
        return false;
    }
单击下载按钮时,我发现文件未找到错误

W/System.err: java.io.FileNotFoundException: /storage/emulated/0/TirunelveliParithabangal/1619611629915.jpg: open failed: ENOENT (No such file or directory)

我不知道怎么解决这个问题。请帮助我找到一些解决方案。

如果您想使用比

dependencies {
        implementation 'com.github.artjimlop:altex-image-downloader:'
    }
并使用此代码下载

AltexImageDownloader.writeToDisk(context, PICTURE_URL, NAME_FOLDER); //NAME_FOLDER is the name of the folder where you want to save the image.

只需获取url并将其替换为
PICTURE\uURL

尝试使用此url从url下载图像,这对我很有帮助,即使您在通知栏上获得了下载。您可以在调用时设置文件名

 public static void downloadImage(Context context, String filename, String downloadUrlOfImage){
        try{
            DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
            Uri downloadUri = Uri.parse(downloadUrlOfImage);
            DownloadManager.Request request = new DownloadManager.Request(downloadUri);
            request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
                    .setAllowedOverRoaming(false)
                    .setTitle("result_"+filename)
                    .setMimeType("image/jpeg") // Your file type. You can use this code to download other file types also.
                    .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
                    .setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES,File.separator+"TirunelveliParithabangal"+File.separator + filename + ".jpg");
            dm.enqueue(request);
            Log.d(TAG, "Image download started");
        }catch (Exception e){
            Log.d(TAG, "Image download failed.");
            Toast.makeText(context, "Image download failed: "+filename, Toast.LENGTH_SHORT).show();
        }
    }