Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.arter97.imagedump;
- import androidx.annotation.Nullable;
- import androidx.appcompat.app.AppCompatActivity;
- import android.graphics.Bitmap;
- import android.graphics.Canvas;
- import android.graphics.drawable.BitmapDrawable;
- import android.graphics.drawable.Drawable;
- import android.os.Bundle;
- import android.util.Base64;
- import android.widget.ImageView;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.ArrayList;
- public class MainActivity extends AppCompatActivity {
- private ImageView imageView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- imageView = findViewById(R.id.imageView);
- try {
- this.setImageDrawable(null);
- } catch (Exception e) {
- // Do nothing
- }
- }
- public Bitmap drawableToBitmap(Drawable drawable) {
- Bitmap bitmap;
- if (drawable instanceof BitmapDrawable) {
- BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
- if (bitmapDrawable.getBitmap() != null) {
- return bitmapDrawable.getBitmap();
- }
- }
- if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
- bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
- } else {
- bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
- }
- Canvas canvas = new Canvas(bitmap);
- drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
- drawable.draw(canvas);
- return bitmap;
- }
- private ArrayList<String> dupBitmap = new ArrayList<>();
- public void setImageDrawable(@Nullable Drawable drawable) throws IOException {
- ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
- String path = "/sdcard/KakaoDump/emoji_" + System.currentTimeMillis() + ".png";
- drawableToBitmap(drawable).compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
- byte[] byteArray = byteArrayOutputStream.toByteArray();
- String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
- if (!dupBitmap.contains(encoded)) {
- dupBitmap.add(encoded);
- FileOutputStream out = new FileOutputStream(new File(path));
- byteArrayOutputStream.writeTo(out);
- out.close();
- }
- imageView.setImageDrawable(drawable);
- }
- }
Add Comment
Please, Sign In to add comment