使用相机和Android Image Cropper库时如何解决以下错误

时间:2019-03-03 05:09:23

标签: android image camera

使用相机和Android Image Cropper库时如何解决以下错误:

拍照后,它会重新打开相机和裁剪图像。

这将为相机打开两次。谁能说出问题在哪里? 我无法解决此错误。 请帮我。

配置:

compileSdkVersion 28
minSdkVersion 18
targetSdkVersion 28

依赖性:

implementation 'com.theartofdev.edmodo:android-image-cropper:2.8.0'

我的Permissioncamera代码:

if (checkSelfPermission(Manifest.permission.CAMERA)!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA}, MY_CAMERA_REQUEST_CODE);
}
else
{                                                  
Intent camera_intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File file=new File(Environment.getExternalStorageDirectory(),"file"+String.valueOf(System.currentTimeMillis()+".jpg"));
Uri uri = FileProvider.getUriForFile(NewAdActivity.this, BuildConfig.APPLICATION_ID + ".provider",file);
camera_intent.putExtra("return-data", true);
camera_intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(camera_intent,1);
}

onActivityResult

public void onActivityResult(int requestCode,int resultCode,Intent data)
{
if (requestCode==1&&resultCode==RESULT_OK)       //camera
{
CropImage.activity(uri).setAspectRatio(1,1).setRequestedSize(512,512).start(this);
}
else  if (requestCode==2&&resultCode==RESULT_OK) {     //gallery
uri=data.getData();
CropImage.activity(uri).setAspectRatio(1,1).setRequestedSize(512,512).start(this);
}

else if (requestCode==CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE&&resultCode==RESULT_OK)
{
CropImage.ActivityResult result = CropImage.getActivityResult(data);
 Uri resutlUri=result.getUri();
images.get(current_image).setImageURI(resutlUri);
fill_images[current_image]=true;
BitmapDrawable bd=((BitmapDrawable) images.get(current_image).getDrawable());
Bitmap bm=bd.getBitmap();
ByteArrayOutputStream bao=new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG,90,bao);
image_base64=Base64.encodeToString(bao.toByteArray(),Base64.DEFAULT);
checkConnection();
new upload_image().execute();
}
}

1 个答案:

答案 0 :(得分:0)

我有同样的问题。我猜问题是在CameraActivity返回一些结果之后,开始了CropImage活动。这就是为什么相机要打开两次的原因。您可以尝试这样的事情:

CropImage.activity().setGuidelines(CropImageView.Guidelines.ON).start((Activity) view.getContext());

此后,我通过覆盖onActivityResult()获得了作为位图的图像:

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == RESULT_OK){
         if(requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE){
            CropImage.ActivityResult result = CropImage.getActivityResult(data);

            Bitmap bmp = null; 

                try {
                    InputStream is = context.getContentResolver().openInputStream(result.getUri());
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    bmp = BitmapFactory.decodeStream(is, null, options);

                } catch (Exception ex) {
                    Log.i(getClass().getSimpleName(), ex.getMessage());
                    Toast.makeText(context, errorConvert, Toast.LENGTH_SHORT).show();
                }
        }
    }
}

最后,您的bmp变量将包含位图图像,您可以根据需要对其进行处理。

它对我有用,希望它也对您有用,祝您好运!