下面是我用来在我的android项目中通过import android.util.Base64将图像文件从位图编码为base64字符串的代码部分:
Bitmap img_bmp=BitmapFactory.decodeStream(getContentResolver().
openInputStream(this.browseImageURI));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
img_bmp.compress(Bitmap.CompressFormat.JPEG, 30, baos);
byte[] image = baos.toByteArray();
String profile_img = Base64.encodeToString(image, Base64.DEFAULT);
`字符串profile_img将作为字符串保存在mysql数据库中 . 当我从数据库中检索字符串值时,我将使用以下代码将图像字符串从String解码为Bitmap:
`
Intent i = getIntent(); //pass the value from previous activity
str_img= i.getStringExtra("img");
img_bm = StringToBitMap(str_img);
imgview = (ImageView)findViewById(R.id.imageView1);
imgview.setImageBitmap(img_bm); // display the image
//Function to convert string to bitmap
public Bitmap StringToBitMap(String image){
try{
byte [] encodeByte=Base64.decode(image,Base64.DEFAULT);
Bitmap bitmap=BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
return bitmap;
}catch(Exception e){
e.getMessage();
return null;
}
}
`我希望它会显示图像,但图像不显示,我从Base64解码器得到一条日志信息“--- decoder-> decode returns false” .
有人可以帮我弄清楚我的代码有什么问题吗?
并且有人知道如何将base64字符串图像(从JSON传递到php脚本)转换为blob格式,以便我可以将其存储为mysql中的BLOB . 先感谢您 .