2016-06-08 83 views
0

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

我们如何才能做到这一点?

+0

的可能的复制[如何使用一个滑行下载图像成位图?](http://stackoverflow.com/questions/27394016/how-一次性使用滑动下载图像到位图) –

回答

0
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 
     } 
    }; 
+0

我不想将图像设置为imageview并一次保存到图库。其实我有一个recylcerview所有图像。每个recyclerview项目都可以选择下载该图像。在选择下载选项时,我想从ImageView中检索图像(可能是Bitmap),以便我可以将其保存到图库中。 – Chetan

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并不总是一个instanceof 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); 
     } 

试试这个

+0

使用Glide库.... – Cliff

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