我的应用中遇到问题:
我必须从相机或图库中获取照片,将其显示在一个小的imageView中并以base64格式发送到服务器(我不需要将图像保存为手机中的文件)。
我可以拍照但质量很差! 互联网上找到的解决方案对我不起作用 你能救我吗?
这是代码
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case 0:
if(resultCode == RESULT_OK){
bitmap_photo = (Bitmap) imageReturnedIntent.getExtras().get("data");
Bitmap newBitmap = Bitmap.createScaledBitmap(bitmap_photo,
70, 70, true);
userImage.setImageBitmap(newBitmap);
bitmap_photo = newBitmap;
}
break;
case 1:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
userImage.setImageURI(selectedImage);
try {
bitmap_photo = MediaStore.Images.Media.getBitmap(this.getContentResolver(),selectedImage);
Bitmap newBitmap = Bitmap.createScaledBitmap(bitmap_photo,
70, 70, true);
bitmap_photo = newBitmap;
} catch (IOException e) {
e.printStackTrace();
}
}
break;
}
}
这是将图像转换为base64的代码:
public String getBase64Image(){
if(bitmap_photo!=null){
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap_photo.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream .toByteArray();
String base64image = Base64.encodeToString(byteArray, Base64.NO_WRAP);
return base64image;
} else {
return "";
}
}
谢谢你!