Android 将Base 64字符串解码为位图失败
我正在进行一个项目。我想将一个图像从我的应用程序上载到服务器。我将图像从imageview编码并上载到服务器作为Base64字符串,然后将其解码回imageview中显示。但我面临将Base64字符串解码回位图的问题。我正在Android 将Base 64字符串解码为位图失败,android,bitmap,base64,Android,Bitmap,Base64,我正在进行一个项目。我想将一个图像从我的应用程序上载到服务器。我将图像从imageview编码并上载到服务器作为Base64字符串,然后将其解码回imageview中显示。但我面临将Base64字符串解码回位图的问题。我正在 Caused by: java.lang.IllegalArgumentException: bad base-64 我用于编码和解码的代码如下所示: 对于编码: Uri selectedImage = data.getData(); String
Caused by: java.lang.IllegalArgumentException: bad base-64
我用于编码和解码的代码如下所示:
对于编码:
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
System.out.println("PicturePath:"+picturePath);
imgImage.setImageBitmap(BitmapFactory.decodeFile(picturePath));
Bitmap bitmapOrg = BitmapFactory.decodeFile(picturePath);
System.out.println("15102015 bitmap:" + bitmapOrg.toString());
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 100, bao);
byte [] ba = bao.toByteArray();
ba1= Base64.encode(ba, Base64.DEFAULT).toString();
解码:
byte [] encodeByte=Base64.decode(model.getmImage().toString(), Base64.DEFAULT);
System.out.println("15102015 encodeByte:" + encodeByte.toString());
Bitmap bitmap;
try{
bitmap=BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
imgImage.setImageBitmap(bitmap);
}catch(Exception e){
System.out.println("15102015 Exception e:" + e.toString());
}
其中model.getmImage().toString()具有Base64字符串值
请帮助我,因为我在过去的几天里一直在支持这个问题。我一定会核实你的答案
提前谢谢 你能添加你的Base64编码字符串吗@ARSALIAM这是编码字符串:[B@29cec3f3This不是图像的有效编码字符串这就是它抛出bad Base64 Exception的原因
model.getmImage()的值是多少?这不是字符串吗?检查它是否已压缩?