How to convert bitmap to PNG and then to base64 in Android?

前端 未结 1 1259
长情又很酷
长情又很酷 2021-02-05 14:16

As the title implies, I\'m trying to get the user of my Android app to select an image from his device (done), I then want to scale the image down (done), compress/convert the i

1条回答
  •  梦谈多话
    2021-02-05 14:59

    Try this to convert bitmap into png:

     bitmap.compress(Bitmap.CompressFormat.PNG, quality, outStream);
    

    Check method's documentation.

    You can directly convert bitmap to Base64. Use this for encoding and decoding from and to Base64.

    public static String encodeToBase64(Bitmap image)
    {
        Bitmap immagex=image;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();  
        immagex.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] b = baos.toByteArray();
        String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);
    
        Log.e("LOOK", imageEncoded);
        return imageEncoded;
    }
    
    public static Bitmap decodeBase64(String input) 
    {
        byte[] decodedByte = Base64.decode(input, 0);
        return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length); 
    }
    

    0 讨论(0)
提交回复
热议问题