2016-12-30 47 views
1

It's my MySql DBits my php page that is read streareader 其ecnoding: -如何將圖像轉換爲android中的字符串?

public String convertBitmapToString(Bitmap bmp) { 
    ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
     bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); //compress to which format you want. 
     byte[] byte_arr = stream.toByteArray(); 
     String imageStr = Base64.encodeToString(byte_arr, 1); 
     return imageStr; 
} 

這是解碼: -

String img=o.toString(); 
    byte[] imageAsBytes = Base64.decode(img.getBytes(), Base64.DEFAULT); 
imageView.setImageBitmap(BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)); 

謝謝。

+0

可以做到,那麼,什麼是錯的代碼? –

+0

你的意思是?我沒有看到任何問題 – Ghorbanzadeh

+0

什麼是o參考? –

回答

3

它可以通過

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% 
    bitmap.compress(Bitmap.CompressFormat.JPEG, 40, byteArrayOutputStream); 
    byte[] byteArray = byteArrayOutputStream.toByteArray(); 

    return Base64.encodeToString(byteArray, Base64.DEFAULT); 
} 

而對於解碼

private void decodeBase64AndSetImage(String completeImageData, ImageView imageView) { 

    // Incase you're storing into aws or other places where we have extension stored in the starting. 
    String imageDataBytes = completeImageData.substring(completeImageData.indexOf(",")+1); 

    InputStream stream = new ByteArrayInputStream(Base64.decode(imageDataBytes.getBytes(), Base64.DEFAULT)); 

    Bitmap bitmap = BitmapFactory.decodeStream(stream); 

    imageView.setImageBitmap(bitmap); 
} 
+0

我的解碼代碼是正確的? –

+0

BitmapFactory.decodeStream(new BufferedInputStream(stream));); –

+0

BitmapFactory.decodeStream(new BufferedInputStream(stream));); decodeStream其錯誤 –

相關問題