从相机Android

时间:2017-01-21 18:16:53

标签: android image android-camera crop android-gallery

我正面临一个问题,即在我从图库或相机中挑选图像后尝试裁剪图像时。

当我从图库中选择图像时,它会被正确裁剪而没有任何问题。当我从相机中选择图像时,裁剪已正确完成,但它显示了一个"中间"屏幕如下图所示,没有直接将我带到裁剪屏幕。

enter image description here

注意:上图仅用于表示目的,以显示"中间"最近的屏幕。

我不想要这个"中间"最近显示的屏幕。相反,我想直接导航到裁剪意图。在像Whatsapp这样的应用程序中,当有人更改他/她的个人资料照片时,不会看到此行为。它直接导航到作物意图而不显示"中间"屏幕。

有人可以帮助解决这个问题吗?

这是我的完整代码

 /**
    * This activity is used to display profile picture and update the same
    *
  */
  public class ChangeProfilePhotoActivity extends BaseActivity implements       
  View.OnClickListener {

private TextView mTvTitle;
private ImageView mBtnChangePhoto;
private ImageView mIvProfileImage;
public static final int CROP_PIC_REQUEST_CODE = 3;
public static final int TAKE_PICTURE = 1;
public static final int GALLERY_IMAGE = 2;
public static final int IMAGE_CHOOSER_INTENT = 4;

int PERMISSION_ALL = 10;
String[] PERMISSIONS = { Manifest.permission.CAMERA};

//private Uri mImageUri;
//private Uri mCropImageUri;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile_photo);
    initUI();
}


@Override
protected void onResume() {
    super.onResume();
}

@Override
protected void onPause() {
    super.onPause();
}

@Override
protected void onDestroy() {
    super.onDestroy();
}


private void initUI() {
    mBtnChangePhoto = (ImageView) findViewById(R.id.btnChangePhoto);
    mIvProfileImage = (ImageView) findViewById(R.id.profile_image);

    mBtnChangePhoto.setOnClickListener(this);

}

@Override
public void onClick(View v) {

    switch (v.getId()) {

        case R.id.btnChangePhoto:

            //launch intent chooser
            getPickImageIntent(this);


            break;
        default:
            break;

    }

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    try {
        if (resultCode == Activity.RESULT_OK) {
            switch (requestCode) {
                case IMAGE_CHOOSER_INTENT:
                    if(data!=null){
                        String action = data.getAction();

                        Uri selectedImageUri = data.getData();
                        //send image for cropping
                        doCrop(selectedImageUri);


                    }
                    break;
                case CROP_PIC_REQUEST_CODE:
                    if(data!=null){
                        Bundle extras = data.getExtras();
                        if(extras != null) {
                            Bitmap bmp = (Bitmap) extras.get("data");
                            mIvProfileImage.setImageBitmap(bmp);
                        }
                    }
                    break;

                default:
                    break;
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}


private void doCrop(Uri picUri) {
    try {

        Intent cropIntent = new Intent("com.android.camera.action.CROP");
        cropIntent.setDataAndType(picUri, "image/*");
        cropIntent.putExtra("crop", "true");
        cropIntent.putExtra("aspectX", 1);
        cropIntent.putExtra("aspectY", 1);
        cropIntent.putExtra("outputX", 128);
        cropIntent.putExtra("outputY", 128);
        cropIntent.putExtra("return-data", true);
        startActivityForResult(cropIntent, CROP_PIC_REQUEST_CODE);
    }
    // respond to users whose devices do not support the crop action
    catch (ActivityNotFoundException anfe) {
        // display an error message
        String errorMessage = "Your device doesn't support the crop action!";
        Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
        toast.show();
    }
}

public void getPickImageIntent(Context context) {
    Intent chooserIntent = null;

    List<Intent> intentList = new ArrayList<>();

    Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    //startActivityForResult(pickIntent, GALLERY_IMAGE);

    Intent takePhotoIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
   /* String imageFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/crop_image.jpg";
    File imageFile = new File(imageFilePath);
    Uri picUri = Uri.fromFile(imageFile); // convert path to Uri
    takePhotoIntent.putExtra( MediaStore.EXTRA_OUTPUT, picUri );*/
    //takePhotoIntent.putExtra("return-data", false);
    //startActivityForResult(takePhotoIntent, TAKE_PICTURE);

    intentList = addIntentsToList(context, intentList, pickIntent);
    intentList = addIntentsToList(context, intentList, takePhotoIntent);

    if (intentList.size() > 0) {
        chooserIntent = Intent.createChooser(intentList.remove(intentList.size() - 1),
                context.getString(R.string.pick_image_intent_text));
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentList.toArray(new Parcelable[intentList.size()]));
    }

    startActivityForResult(chooserIntent, IMAGE_CHOOSER_INTENT);

}

private static List<Intent> addIntentsToList(Context context, List<Intent> list, Intent intent) {
    try {
        List<ResolveInfo> resInfo = context.getPackageManager().queryIntentActivities(intent, 0);
        for (ResolveInfo resolveInfo : resInfo) {
            String packageName = resolveInfo.activityInfo.packageName;
            Intent targetedIntent = new Intent(intent);
            targetedIntent.setPackage(packageName);
            list.add(targetedIntent);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return list;
  }
} 

1 个答案:

答案 0 :(得分:3)

您正在启动意图。这意味着您将控制权移交给另一个应用程序。在那之后,你无法控制发生的事情。当然,每个设备上发生的情况都会有所不同,因为一些OEM会使用他们自己的Gallery应用程序或可能表现完全不同的相机应用程序。

所以不,没有办法阻止他们做那个额外的屏幕。如果您想要完全控制,请通过相机或camera2 apis自己拍照,而不是调用Intent。 (注意:我实际上并没有建议这一点,但如果你想控制它是唯一的方法)。