arter97

setImageDrawable dump

Sep 8th, 2019
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.68 KB | None | 0 0
  1. package com.arter97.imagedump;
  2.  
  3. import androidx.annotation.Nullable;
  4. import androidx.appcompat.app.AppCompatActivity;
  5.  
  6. import android.graphics.Bitmap;
  7. import android.graphics.Canvas;
  8. import android.graphics.drawable.BitmapDrawable;
  9. import android.graphics.drawable.Drawable;
  10. import android.os.Bundle;
  11. import android.util.Base64;
  12. import android.widget.ImageView;
  13.  
  14. import java.io.ByteArrayOutputStream;
  15. import java.io.File;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import java.util.ArrayList;
  19.  
  20. public class MainActivity extends AppCompatActivity {
  21.     private ImageView imageView;
  22.  
  23.     @Override
  24.     protected void onCreate(Bundle savedInstanceState) {
  25.         super.onCreate(savedInstanceState);
  26.         setContentView(R.layout.activity_main);
  27.  
  28.         imageView = findViewById(R.id.imageView);
  29.         try {
  30.             this.setImageDrawable(null);
  31.         } catch (Exception e) {
  32.             // Do nothing
  33.         }
  34.     }
  35.  
  36.     public Bitmap drawableToBitmap(Drawable drawable) {
  37.         Bitmap bitmap;
  38.  
  39.         if (drawable instanceof BitmapDrawable) {
  40.             BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
  41.             if (bitmapDrawable.getBitmap() != null) {
  42.                 return bitmapDrawable.getBitmap();
  43.             }
  44.         }
  45.  
  46.         if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
  47.             bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
  48.         } else {
  49.             bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
  50.         }
  51.  
  52.         Canvas canvas = new Canvas(bitmap);
  53.         drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
  54.         drawable.draw(canvas);
  55.         return bitmap;
  56.     }
  57.  
  58.     private ArrayList<String> dupBitmap = new ArrayList<>();
  59.  
  60.     public void setImageDrawable(@Nullable Drawable drawable) throws IOException {
  61.         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  62.         String path = "/sdcard/KakaoDump/emoji_" + System.currentTimeMillis() + ".png";
  63.         drawableToBitmap(drawable).compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
  64.         byte[] byteArray = byteArrayOutputStream.toByteArray();
  65.         String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
  66.  
  67.         if (!dupBitmap.contains(encoded)) {
  68.             dupBitmap.add(encoded);
  69.  
  70.             FileOutputStream out = new FileOutputStream(new File(path));
  71.             byteArrayOutputStream.writeTo(out);
  72.             out.close();
  73.         }
  74.  
  75.         imageView.setImageDrawable(drawable);
  76.     }
  77. }
Add Comment
Please, Sign In to add comment