【问题标题】:How to save an image attached to the image view?如何保存附加到图像视图的图像?
【发布时间】:2016-09-23 22:15:30
【问题描述】:

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

我们怎样才能做到这一点?

【问题讨论】:

标签: android android-glide


【解决方案1】:
Glide.with(yourApplicationContext))
    .load(youUrl)
    .asBitmap()
    .into(new SimpleTarget<Bitmap>(myWidth, myHeight) {
        @Override
        public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
            //set bitmap to imageview and save 
        }
    };

【讨论】:

  • 我不想一次将图像设置为 imageview 并保存到画廊。实际上,我对所有图像都有一个 recyclerview。 recyclerview 的每个项目都有下载该图像的选项。在选择下载选项中,我想从 ImageView 中检索图像(可能是位图),以便将其保存到图库。
【解决方案2】:
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);
        }

试试这个

【讨论】:

    【解决方案3】:

    我没有尝试过这种方式。但我认为这符合你的问题。将此代码放在 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);
                            }
                        });
            }
        };
    

    【讨论】:

      【解决方案4】:

      这可能对你有帮助

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-17
        • 1970-01-01
        相关资源
        最近更新 更多