簡體   English   中英

從 URL android 下載 imageview 中加載的圖像

[英]Download image loaded in imageview from URL android

我從URLimage image view中加載了圖像。 我想下載圖像並將其保存到外部存儲中。

在這里,我使用GlideURL加載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);

接下來,當用戶單擊下載按鈕時,我想將image view中加載的image下載到外部存儲。

這是我使用的代碼:

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:

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

單擊下載按鈕時,我收到 File notFound 錯誤。

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

我不知道如何解決這個問題。 請幫我解決一些問題。

嘗試使用它從 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();
        }
    }

如果你想使用一個庫而不是

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_URL

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM