我在android中遇到了同样的问题,确切的问题是什么_当我将图像编码为其对应Base64
的&如果图像大小更大(2mb或更多......&也取决于图像质量和相机质量,可能取自2MP或 5MP 或可能是 8MP 相机)然后将完整图像转换为 Base64 会遇到问题...您必须减小关注图像的大小!我已经完成了我的工作Android code
_
获取 Base64 图像字符串
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Bitmap mBitmap= new decodeFile("<PATH_OF_IMAGE_HERE>");
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
int i=b.length;
String base64ImageString=android.util.Base64.encodeToString(b, 0, i, android.util.Base64.NO_WRAP);
转换为正确的位图
/**
*My Method that reduce the bitmap size.
*/
private Bitmap decodeFile(String fPath){
//Decode image size
BitmapFactory.Options opts = new BitmapFactory.Options();
//opts.inJustDecodeBounds = true;
opts.inDither=false; //Disable Dithering mode
opts.inPurgeable=true; //Tell to gc that whether it needs free memory, the Bitmap can be cleared
opts.inInputShareable=true; //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
opts.inTempStorage=new byte[1024];
BitmapFactory.decodeFile(fPath, opts);
//The new size we want to scale to
final int REQUIRED_SIZE=70;//or vary accoding to your need...
//Find the correct scale value. It should be the power of 2.
int scale=1;
while(opts.outWidth/scale/2>=REQUIRED_SIZE && opts.outHeight/scale/2>=REQUIRED_SIZE)
scale*=2;
//Decode with inSampleSize
opts.inSampleSize=scale;
return BitmapFactory.decodeFile(fPath, opts);
}
我希望这会帮助其他面临同样问题的朋友......谢谢!