2016-06-08 85 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 
    } 
}