Android下载,保存图片(HttpURLConnection,Fresco)

时间:2025-01-31 08:09:38

1⃣️下载图片

先上高端的Fresco下载的用法:(实际是缓存url的图片到本地了,下次加载同一个url会直接加载本地缓存的这个bitmap)
private void saveImageFromUrl(String url, Context context, final int i) {
final ImageRequest imageRequest =        ((url))            .setProgressiveRenderingEnabled(true).build();

   DataSource<CloseableReference<CloseableImage>> dataSource = ()
                .fetchDecodedImage(imageRequest, context);

        (new BaseBitmapDataSubscriber() {

            @Override
            public void onNewResultImpl(Bitmap bitmap) {
   //形参的bitmap即Fresco缓存到内存的Bitmap
           //  saveBitmap(bitmap);
   //下面是获取fresco缓存到磁盘的cnt图片的路径
CacheKey cacheKey = ()
                .getEncodedCacheKey(imageRequest, this);
BinaryResource resource = ().getMainFileCache().getResource(cacheKey);
if (resource == null) return;
File file = ((FileBinaryResource) resource).getFile();
if (file == null) return;
("wxbnbb", "saveImageFromUrl: " + ());
            }

            @Override
            public void onFailureImpl(DataSource dataSource) {

            }
        }, ());
    }
再上普通的HttpURLConnection下载的用法:
 public void savePhoto() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                url2bitmap(picUrl);
            }
        }).start();
    }
 /**
     * url转bitmap对象
     *
     * @param url
     */
    public void url2bitmap(String url) {
        Bitmap bm = null;
        try {
            URL iconUrl = new URL(url);
            URLConnection conn = ();
            HttpURLConnection http = (HttpURLConnection) conn;
            int length = ();
            ();
            //获得图像的字符流
            InputStream is = ();
            BufferedInputStream bis = new BufferedInputStream(is, length);
            bm = (bis);
            ();
            ();
            if (bm != null) {
                saveBitmap(bm);
            }
        } catch (Exception e) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    (context, "保存失败", Toast.LENGTH_SHORT).show();
                }
            });
            ();
        }
    }

2⃣️用上面两种方法下载完bitmap后保存:

/**
     * 保存bitmap到本地
     *
     * @param bitmap
     */
    private void saveBitmap(Bitmap bitmap) {
        File appDir = new File((), "图片文件夹");
        if (!()) ();
        String[] str = ("/");
        String fileName = str[ - 1];
        File file = new File(appDir, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            (, 100, fos);
            ();
            ();
       //();//获取保存的图片的文件名
        //    onSaveSuccess(file);
        } catch (IOException e) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    (context, "保存失败", Toast.LENGTH_SHORT).show();
                }
            });
            ();
        }
    }

备注:如果要通知相册更新
 private void onSaveSuccess(final File file) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, (file)));
                (context, "保存成功", Toast.LENGTH_SHORT).show();
            }
        });
    }

fresco的磁盘缓存数据并不会随着app的删除而自动清理