android bitmap string,Android Bitmap到Base64字符串(Android Bitmap to Base64 String)
Android Bitmap到Base64字符串(Android Bitmap to Base64 String)
如何將一個大的Bitmap(用手機(jī)相機(jī)拍攝的照片)轉(zhuǎn)換為Base64 String?
How do I convert a large Bitmap (photo taken with the phone's camera) to a Base64 String?
原文:https://stackoverflow.com/questions/9224056
更新時間:2019-11-21 18:31
最滿意答案
使用以下方法將位圖轉(zhuǎn)換為字節(jié)數(shù)組:
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream .toByteArray();
從字節(jié)數(shù)組中使用以下方法對base64進(jìn)行編碼
String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
use following method to convert bitmap to byte array:
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream .toByteArray();
to encode base64 from byte array use following method
String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
2013-12-12
相關(guān)問答
假設(shè)您的圖像數(shù)據(jù)位于一個名為myImageData的字符串中,以下內(nèi)容應(yīng)該可以做到這一點: byte[] imageAsBytes = Base64.decode(myImageData.getBytes(), Base64.DEFAULT);
ImageView image = (ImageView)this.findViewById(R.id.ImageView);
image.setImageBitmap(
BitmapFactory.deco
...
public static String encodeToBase64(Bitmap image, Bitmap.CompressFormat compressFormat, int quality)
{
ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
image.compress(compressFormat, quality, byteArrayOS);
return Base64.enc
...
使用以下方法將位圖轉(zhuǎn)換為字節(jié)數(shù)組: ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream .toByteArray();
從字節(jié)數(shù)組中使用以下方法對base64進(jìn)行編碼 St
...
您只需使用其他內(nèi)置方法即可恢復(fù)代碼。 byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
You can just basically revert your code using some other built in meth
...
我發(fā)現(xiàn)了問題。 問題在于logcat來自我復(fù)制編碼字符串的地方。 Logcat沒有顯示整個String,因此破碎的字符串沒有為我解碼圖像。 因此,當(dāng)我將編碼的字符串直接傳遞給解碼函數(shù)時,圖像是可見的。 I have found out the problem. The problem was with the logcat from where i was copying the encoded string. Logcat didn't display the entire String so
...
試試這個位圖; public Bitmap convert(String img){
byte[] b = Base64.decode(img, Base64.DEFAULT);
return BitmapFactory.decodeByteArray(b, 0, b.length);
}
而這就是String public String convert(Bitmap bm, int quality){
ByteArrayOutputStream baos = n
...
表示圖像的前三個base64字符串解碼正常。 但是接下來的四個產(chǎn)生了一個bad base-64在線的捕獲 byte[] decodedString = Base64.decode(photo, Base64.URL_SAFE);
之前我說過。 如果你愿意的話 byte[] decodedString = Base64.decode(photo, Base64.DEFAULT);
然后沒有捕獲。 所有7個base64字符串解碼都可以。 The first three base64 strings
...
如果你已經(jīng)在文件中有了它,那么就沒有理由對它進(jìn)行解壓縮然后重新壓縮 - 這可能是導(dǎo)致錯誤的原因,因為每次壓縮都是有損的并且會導(dǎo)致數(shù)據(jù)進(jìn)一步丟失。 如果您已有圖像文件,請將其作為原始字節(jié)和Base64讀取。 If you already have it in the file, there is no reason to decompress it then recompress it- which may be the cause of your errors, as each compressi
...
你可以使用httpmime庫上傳任何文件(圖像,音頻,視頻等..)請參考下面的代碼。 HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost postRequest = new HttpPost(your url);
MultipartEntity reqEntity = new MultipartEntity(
Htt
...
只需將base64字符串轉(zhuǎn)換為位圖,而不是使用下面的代碼將該位圖加載到imageview中 byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
image.setImageBitmap(decodedByte);
Just con
...
總結(jié)
以上是生活随笔為你收集整理的android bitmap string,Android Bitmap到Base64字符串(Android Bitmap to Base64 String)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 监听布局改变,Andro
- 下一篇: ps怎么擦去(ps怎么擦去图片上的文字)