任何人都可以告诉我如何将任何格式图像直接转换为字符串?

问题描述 投票:-1回答:1
private String getBase64String() {

    // give your image file url in mCurrentPhotoPath
    Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath);

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    // In case you want to compress your image, here it's at 40%

// here i use JPEG image but now can anyone tell how can i convert any format image in String 
    bitmap.compress(Bitmap.CompressFormat.JPEG, 40, byteArrayOutputStream);
    byte[] byteArray = byteArrayOutputStream.toByteArray();

    return Base64.encodeToString(byteArray, Base64.DEFAULT);
}
android string base64
1个回答
0
投票

您可以使用Base64 Android类:

String ecodImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);

你必须将你的图像转换为字节数组。喜欢:

Bitmap btm = BitmapFactory.decodeFile("/path/to/image.jpg");
ByteArrayOutputStream baos = new ByteArrayOutputStream();  
btm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //btm is the bitmap object   
byte[] b = baos.toByteArray(); 
© www.soinside.com 2019 - 2024. All rights reserved.