从相机捕获图像后,应用程序停止并显示logcat中显示的错误。我认为这与捕获的图像大小有关。我需要在将图像上传到服务器之前压缩图像。 这是相关代码
private void openCameraIntent() {
createImageFile();
Utils.getInstance().openCamera(this,profileImageFile);
}
private void createImageFile() {
try{
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
profileImageFile = Environment.getExternalStorageDirectory()
+ "/StoredImages/"+"img" + timeStamp + ".jpeg";
}catch(Exception e){
e.printStackTrace();
}
}
openCamera方法
public void openCamera(Fragment fragment,String mediaFile) {
File mediaStorageDir = new File(
Environment.getExternalStorageDirectory()
+ "/StoredImages/");
if (!mediaStorageDir.exists()) {
mediaStorageDir.mkdirs();
}
try{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri camCapfileUri = Uri.fromFile(new File(mediaFile));
intent.putExtra(MediaStore.EXTRA_OUTPUT, camCapfileUri);
fragment.startActivityForResult(intent, StringConstants.CAMERA_PERMISSION_REQUEST_CODE);
}catch (Exception e){
e.printStackTrace();
}
}
onActivityResult
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == StringConstants.CAMERA_PERMISSION_REQUEST_CODE &&
resultCode == Activity.RESULT_OK){
onCameraImageCapture();
}else if(requestCode == StringConstants.PERMISSION_REQUESTCODE_GALLERY && resultCode == Activity.RESULT_OK){
onGalleryImageSelected(data);
}
}
捕获图像后,它会像这样转换为字符串
private void onCameraImageCapture() {
profileBitMap = BitmapFactory.decodeFile(profileImageFile);
String x = Utils.getInstance().encodeToBase64(profileBitMap,Bitmap.CompressFormat.JPEG,100);
uploadProfileImage();
}
public static String encodeToBase64(Bitmap image, Bitmap.CompressFormat compressFormat, int quality) {
try{
ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
image.compress(compressFormat, quality, byteArrayOS);
return Base64.encodeToString(byteArrayOS.toByteArray(), Base64.DEFAULT);
}catch(Exception e){
e.printStackTrace();
}
return null;
}