使用Glide将图片保存到存储 [英] Save picture to storage using Glide

查看:1004
本文介绍了使用Glide将图片保存到存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向我的应用添加下载功能,我正在使用 Glide Library 从其 URL 加载图片,使用下面的代码,我可以将图片保存到设备存储中,但是无法正常使用.

I'm trying to add a Download feature to my app, I'm using Glide Library to load the pictures from their URL, with the code below, I can save the picture to the device storage, but it's not working as I expected.

                    new AsyncTask<FutureTarget<Bitmap>, Void, Bitmap>() {
                        @Override protected Bitmap doInBackground(FutureTarget<Bitmap>... params) {
                            try {
                                return params[0].get();
                            } catch (Exception ex) {
                                return null;
                            }
                        }
                        @Override protected void onPostExecute(Bitmap bitmap) {
                            if(bitmap == null) return;
                            MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "Art_"+System.currentTimeMillis());
                        }
                    }.execute(Glide.with(getApplicationContext()).load(url).asBitmap().diskCacheStrategy(DiskCacheStrategy.SOURCE).into(width, height));

有人可以帮助我解决此问题吗?

can anyone help me to get resolve this issue?

推荐答案

1.清单文件中的权限

1.Permissions in manifest file

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

2.滑动以将图片下载为位图

2.Glide to download the image as a bitmap

Glide.with(mContext)
         .load("YOUR_URL")
         .asBitmap()
         .into(new SimpleTarget<Bitmap>(100,100) {
             @Override
             public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation)  {
                   saveImage(resource);
             }
         });

3.将位图保存到内存中

3. Save the bitmap into your memory

private String saveImage(Bitmap image) {
    String savedImagePath = null;

    String imageFileName = "JPEG_" + "FILE_NAME" + ".jpg";
    File storageDir = new File(            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
                       + "/YOUR_FOLDER_NAME");
    boolean success = true;
    if (!storageDir.exists()) {
    success = storageDir.mkdirs();
    }
    if (success) {
        File imageFile = new File(storageDir, imageFileName);
        savedImagePath = imageFile.getAbsolutePath();
        try {
            OutputStream fOut = new FileOutputStream(imageFile);
            image.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
            fOut.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Add the image to the system gallery
        galleryAddPic(savedImagePath);
        Toast.makeText(mContext, "IMAGE SAVED", Toast.LENGTH_LONG).show();
    }
    return savedImagePath;
}

private void galleryAddPic(String imagePath) {
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    File f = new File(imagePath);
    Uri contentUri = Uri.fromFile(f);
    mediaScanIntent.setData(contentUri);
    sendBroadcast(mediaScanIntent);
}

这篇关于使用Glide将图片保存到存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆