2016-06-08 4 views
0

Ich verwende eine Glide-Bibliothek zum Laden von Remote-URLs in ImageViews. Ich möchte das Bild von diesem ImageView in Galerie speichern. (Ich möchte nicht erneut einen Netzwerkanruf tätigen, um das gleiche Bild herunterzuladen).Wie kann ein an die Bildansicht angehängtes Bild gespeichert werden?

Wie können wir das erreichen?

+0

Mögliche Duplikat [Wie benutzt man glide ein Bild in eine Bitmap herunterladen?] (Http://stackoverflow.com/questions/27394016/how- does-one-use-glide-to-download-ein-Bild-in-ein-Bitmap) –

Antwort

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

Ich möchte Bild nicht auf Bildansicht & speichern in Galerie auf einmal festlegen. Eigentlich habe ich einen Überblick über alle Bilder. Jeder Recycler-View hat die Option, das Image herunterzuladen. Bei der Auswahl-Download-Option möchte ich ein Bild von ImageView (als Bitmap) abrufen, damit ich es in der Galerie speichern kann. – 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"); 
    } 

Hinweis: Wenn Ihr drawble nicht immer eine instanceof BitmapDrawable ist

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

Versuchen Sie, diese

+0

Verwenden der Glide-Bibliothek .... – Cliff

0

ich nicht auf diese Weise versuchen. Aber ich denke, das passt zu deinem Problem. Setzen Sie diesen Code auf onBindViewHolder Ihres RecyclerView-Adapters.

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

Dies könnte Ihnen helfen

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