如何保存附加到图像视图的图像?

时间:2016-06-08 11:26:30

标签: android android-glide

我正在使用Glide库将远程URL加载到ImageView中。 我想将图像从此ImageView保存到图库。 (我不想再次打网络来下载相同的图像)。

我们如何才能实现这一目标?

4 个答案:

答案 0 :(得分:0)

.on()

答案 1 :(得分:0)

BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
   Bitmap bitmap = drawable.getBitmap();
   ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, 200, 200, false);

    scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 60, byteArrayOutputStream);
    String fileName = "image.jpeg";
    File file = new File("your_directory_path/"
            + fileName);
    try {
        file.createNewFile();
        // write the bytes in file
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        fileOutputStream.write(byteArrayOutputStream.toByteArray());
        // remember close the FileOutput stream
        fileOutputStream.close();
        ToastHelper.show(getString(R.string.qr_code_save));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        ToastHelper.show("Error");
    }

注意:如果您的drawble并不总是BitmapDrawable的实例

            Bitmap bitmap;
        if (mImageView.getDrawable() instanceof BitmapDrawable) {
            bitmap = ((BitmapDrawable) mImageView.getDrawable()).getBitmap();
        } else {
            Drawable d = mImageView.getDrawable();
            bitmap = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        }

试试这个

答案 2 :(得分:0)

我没有尝试过这种方式。但我认为这符合您的问题。将此代码放在RecyclerView适配器的 onBindViewHolder 上。

Glide.with(yourApplicationContext))
    .load(youUrl)
    .asBitmap()
    .into(new SimpleTarget<Bitmap>(myWidth, myHeight) {
        @Override
        public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
            //Set bitmap to your ImageView 
            imageView.setImageBitmap(bitmap);

            viewHolder.saveButton.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View arg0) {
                           //Save bitmap to gallery
                           saveToGallery(bitmap);
                        }
                    });
        }
    };

答案 3 :(得分:0)

这可能对您有所帮助

public void saveBitmap(ImageView imageView) {

    Bitmap bitmap = ((GlideBitmapDrawable) imageView.getDrawable()).getBitmap();
    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/My Images");
    myDir.mkdirs();
    Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);
    String fname = "Image-" + n + ".jpg";
    File file = new File(myDir, fname);
    if (file.exists()) file.delete();
    try {
        FileOutputStream out = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
        out.flush();
        out.close();
    } catch (Exception ex) {
        //ignore
    }
}