簡體   English   中英

誰能告訴我如何將任何格式的圖像直接轉換為字符串?

[英]Can anyone tell me how i can convert any format image directly to string?

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

您可以使用 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(); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM