Android的意图有多个选项,也就是,从GALLARY和捕捉图像使用前置摄像头拾取图像图像、多个、意图、摄像头

2023-09-12 22:59:24 作者:丶随风__丶

选择/拍摄图像后,我需要裁剪图像。

After selecting/capturing the image i need to crop the image.

我已经做到了这一点one.but的问题是,当我切换到Android最新版本(奇巧)作物的意​​图不能正常工作。

I already done this one.but the problem is when i am switching to the android latest version(kitkat) crop intent is not working properly.

我的code

    private void picPhoto() {
    Intent pickIntent = new Intent();
    if (Build.VERSION.SDK_INT < 19) {
        pickIntent.setType("image/jpeg");
        pickIntent.setAction(Intent.ACTION_GET_CONTENT);
        pickIntent.putExtra("crop", "true");
    } else {
        pickIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
        pickIntent.addCategory(Intent.CATEGORY_OPENABLE);
        pickIntent.setType("image/jpeg");
        pickIntent.putExtra("crop", "true");
    }
    Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, getFileDirectory());
    takePhotoIntent.putExtra("android.intent.extras.CAMERA_FACING", 1);

    String pickTitle = "Select or take a new Picture";
    Intent chooserIntent = Intent.createChooser(pickIntent, pickTitle);

    chooserIntent.putExtra
            (
                    Intent.EXTRA_INITIAL_INTENTS,
                    new Intent[]{takePhotoIntent}
            );

    startActivityForResult(chooserIntent, PICK_IMAGE);
}

任何一个可以帮助我举个例子?

推荐答案

因此,这是它..

   private void picPhoto() {
    Intent pickIntent = new Intent();
    if (Build.VERSION.SDK_INT < 19) {
        pickIntent.setType("image/jpeg");
        pickIntent.setAction(Intent.ACTION_GET_CONTENT);
    } else {
        pickIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
        pickIntent.addCategory(Intent.CATEGORY_OPENABLE);
        pickIntent.setType("image/jpeg");
    }
    Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, getFileDirectory());
    takePhotoIntent.putExtra("android.intent.extras.CAMERA_FACING", 1);

    String pickTitle = "Select or take a new Picture";
    Intent chooserIntent = Intent.createChooser(pickIntent, pickTitle);

    chooserIntent.putExtra
            (
                    Intent.EXTRA_INITIAL_INTENTS,
                    new Intent[]{takePhotoIntent}
            );

    startActivityForResult(chooserIntent, PICK_IMAGE);
}

所有的功能将工作API> 14即Android 4.0的(ICE_CREAM_SANDWICH)。

    @TargetApi(Build.VERSION_CODES.KITKAT)
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == PICK_IMAGE && data != null && data.getData() != null) {
            Uri _uri = data.getData();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                String wholeID = DocumentsContract.getDocumentId(_uri);
                // Split at colon, use second item in the array
                String id = wholeID.split(":")[1];
                String[] column = {MediaStore.Images.Media.DATA};
                // where id is equal to
                String sel = MediaStore.Images.Media._ID + "=?";
                Cursor cursor = getActivity().getContentResolver().
                        query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                                column, sel, new String[]{id}, null);

                String filePath = "";
                int columnIndex = cursor.getColumnIndex(column[0]);
                if (cursor.moveToFirst()) {
                    filePath = cursor.getString(columnIndex);
                }
                cursor.close();
                croppedPath = filePath;
            } else {
                //User had pick an image.
                Cursor cursor = getActivity().getContentResolver().query(_uri, new String[]{android.provider.MediaStore.Images.ImageColumns.DATA}, null, null, null);
                cursor.moveToFirst();
                //Link to the image
                final String imageFilePath = cursor.getString(0);
                croppedPath = imageFilePath;
                cursor.close();
            }
            if (croppedPath != null) {
                cropIntent(croppedPath, 5);
            }
        } else if (requestCode != 4 && requestCode == PICK_IMAGE && data == null) {
            cropIntent(getFileDirectory().getPath(), 4);
        } else if (requestCode == 4) {
            editPhoto1.setImageBitmap(null);
            if (bmp != null) {
                bmp.recycle();
            }
            setImagePicked(getFileDirectory().getPath(), 0);
        } else if (requestCode == 5) {
            if (croppedPath != null) {
                editPhoto1.setImageBitmap(null);
                if (bmp != null) {
                    bmp.recycle();
                }
                setImagePicked(getFileDirectory().getPath(), 0);
                croppedPath = null;
            }
        }
    }


    super.onActivityResult(requestCode, resultCode, data);
}

裁剪方法

 private void cropIntent(String fileToCrop, int requestCode) {
    Uri filePath = Uri.fromFile(new File(fileToCrop));
    Intent cropIntent = new Intent("com.android.camera.action.CROP");
    //indicate image type and Uri
    cropIntent.setDataAndType(filePath, "image/*");
    cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, getFileDirectory());
    //set crop properties
    cropIntent.putExtra("crop", "true");
    //indicate aspect of desired crop
    cropIntent.putExtra("aspectX", 1);
    cropIntent.putExtra("aspectY", 1);
    //indicate output X and Y
    cropIntent.putExtra("outputX", 256);
    cropIntent.putExtra("outputY", 256);
    //retrieve data on return
    cropIntent.putExtra("return-data", true);
    startActivityForResult(cropIntent, requestCode);
    //start the activity - we handle returning in onActivityResult
}