如何在Android 7.0中从相机或图库选择要裁剪的图像?

16

从相册和摄像头中选择图像进行裁剪,在Android 7.0以下的版本中可以完成,但在Android Nougat中使用摄像头时会崩溃。我使用fileprovider进行操作,但无效。

MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private Button mBtn;
private Context context;
private static final int SELECT_PICTURE_CAMARA = 101, SELECT_PICTURE = 201, CROP_IMAGE = 301;
private Uri outputFileUri;
String mCurrentPhotoPath;
private Uri selectedImageUri;
private File finalFile = null;
private ImageView imageView;
private PermissionUtil permissionUtil;
Uri fileUri;
File file = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mBtn = (Button) findViewById(R.id.btn_img);
    imageView = (ImageView) findViewById(R.id.img_photo);
    permissionUtil = new PermissionUtil();
    mBtn.setOnClickListener(this);
    context = this;
}

@Override
public void onClick(View view) {
    selectImageOption();
}

private void selectImageOption() {
    final CharSequence[] items = {"Capture Photo", "Choose from Gallery", "Cancel"};

    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setTitle("Add Photo!");
    builder.setItems(items, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int item) {
            if (items[item].equals("Capture Photo")) {
                if (permissionUtil.checkMarshMellowPermission()) {
                    if (permissionUtil.verifyPermissions(MainActivity.this, permissionUtil.getCameraPermissions()))
                        onClickCamera();
                    else
                        ActivityCompat.requestPermissions(MainActivity.this, permissionUtil.getCameraPermissions(), SELECT_PICTURE_CAMARA);
                } else
                    onClickCamera();
            } else if (items[item].equals("Choose from Gallery")) {
                if (permissionUtil.checkMarshMellowPermission()) {
                    if (permissionUtil.verifyPermissions(MainActivity.this, permissionUtil.getGalleryPermissions()))
                        onClickGallery();
                    else
                        ActivityCompat.requestPermissions(MainActivity.this, permissionUtil.getGalleryPermissions(), SELECT_PICTURE);
                } else
                    onClickGallery();
            } else if (items[item].equals("Cancel")) {
                dialog.dismiss();
            }
        }
    });
    builder.show();
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            selectedImageUri = data.getData();
            cropImage(selectedImageUri);

        } else if (requestCode == CROP_IMAGE) {

            Uri imageUri = Uri.parse(mCurrentPhotoPath);
            File file = new File(imageUri.getPath());
            try {
                InputStream ims = new FileInputStream(file);
                imageView.setImageBitmap(BitmapFactory.decodeStream(ims));
            } catch (FileNotFoundException e) {
                return;
            }

        } else if (requestCode == SELECT_PICTURE_CAMARA && resultCode == Activity.RESULT_OK) {
            cropImage1();
        }
    }
}

private void onClickCamera() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    if (takePictureIntent.resolveActivity(context.getPackageManager()) != null) {
       /* File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
        }
        if (photoFile != null) {

            Uri photoURI;
            if (Build.VERSION.SDK_INT >= 24) {
                photoURI = FileProvider.getUriForFile(MainActivity.this,
                        BuildConfig.APPLICATION_ID + ".fileprovider", photoFile);
            } else {
                photoURI = Uri.fromFile(photoFile);
            }
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, SELECT_PICTURE_CAMARA);

        }*/

        ContentValues values = new ContentValues(1);
        values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpg");
        fileUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
        takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        startActivityForResult(takePictureIntent, SELECT_PICTURE_CAMARA);

    } else {
        Toast.makeText(this, getString(R.string.error_no_camera), Toast.LENGTH_LONG).show();
    }
}

private void onClickGallery() {
    List<Intent> targets = new ArrayList<>();
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_PICK);
    intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
    List<ResolveInfo> candidates = getApplicationContext().getPackageManager().queryIntentActivities(intent, 0);

    for (ResolveInfo candidate : candidates) {
        String packageName = candidate.activityInfo.packageName;
        if (!packageName.equals("com.google.android.apps.photos") && !packageName.equals("com.google.android.apps.plus") && !packageName.equals("com.android.documentsui")) {
            Intent iWantThis = new Intent();
            iWantThis.setType("image/*");
            iWantThis.setAction(Intent.ACTION_PICK);
            iWantThis.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
            iWantThis.setPackage(packageName);
            targets.add(iWantThis);
        }
    }
    if (targets.size() > 0) {
        Intent chooser = Intent.createChooser(targets.remove(0), "Select Picture");
        chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, targets.toArray(new Parcelable[targets.size()]));
        startActivityForResult(chooser, SELECT_PICTURE);
    } else {
        Intent intent1 = new Intent(Intent.ACTION_PICK);
        intent1.setType("image/*");
        startActivityForResult(Intent.createChooser(intent1, "Select Picture"), SELECT_PICTURE);
    }
}

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";

    File storageDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    if (Build.VERSION.SDK_INT >= 24) {
        mCurrentPhotoPath = String.valueOf(FileProvider.getUriForFile(MainActivity.this,
                BuildConfig.APPLICATION_ID + ".provider", image));
    } else {
        mCurrentPhotoPath = String.valueOf(Uri.fromFile(image));
    }

    return image;
}

/*private Uri createImageUri(){
    ContentResolver contentResolver=getContentResolver();
    ContentValues cv=new ContentValues();
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    cv.put(MediaStore.Images.Media.TITLE,timeStamp);
    return contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,cv);
}*/

private void cropImage(Uri selectedImageUri) {
    Intent cropIntent = new Intent("com.android.camera.action.CROP");
    cropIntent.setDataAndType(selectedImageUri, "image/*");
    cropIntent.putExtra("crop", "true");
    cropIntent.putExtra("aspectX", 1);
    cropIntent.putExtra("aspectY", 1.5);
    cropIntent.putExtra("return-data", true);

    outputFileUri = Uri.fromFile(createCropFile());
    cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
    startActivityForResult(cropIntent, CROP_IMAGE);
}

private void cropImage1() {
    Intent cropIntent = new Intent("com.android.camera.action.CROP");
    cropIntent.setDataAndType(fileUri, "image/*");
    cropIntent.putExtra("crop", "true");
    cropIntent.putExtra("aspectX", 1);
    cropIntent.putExtra("aspectY", 1.5);
    cropIntent.putExtra("return-data", true);

    if (Build.VERSION.SDK_INT >= 24) {
        outputFileUri = FileProvider.getUriForFile(MainActivity.this,
                BuildConfig.APPLICATION_ID + ".provider", createCropFile());

    } else
        outputFileUri = Uri.fromFile(createCropFile());

    cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
    startActivityForResult(cropIntent, CROP_IMAGE);

   /* ContentValues values = new ContentValues(1);
    values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpg");
    outputFileUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
    cropIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
    startActivityForResult(cropIntent, CROP_IMAGE);*/
}

private File createCropFile() {
    File storageDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    //  path = path + (timeStamp + "1jpg");

    try {
        file = File.createTempFile(timeStamp, ".jpg", storageDir);
    } catch (IOException e) {
        e.printStackTrace();
    }

    /*if (Build.VERSION.SDK_INT >= 24)
        mCurrentPhotoPath = String.valueOf(FileProvider.getUriForFile(MainActivity.this,
                BuildConfig.APPLICATION_ID + ".provider", file));
    else*/
    mCurrentPhotoPath = String.valueOf(Uri.fromFile(file));
    return file;
}
}

这个在所有设备上都可以运行,但无法在 Android 7.0 Nougat 及以上版本的设备上运行。


1
请不要重新发布问题。您可以编辑先前的问题以将其推到前台。 - OneCricketeer
@shweta,你得到如何在Android 7及其以下版本中裁剪图像的答案了吗? - Atul Dhanuka
是的,获得了解决方案。 - Shweta Chauhan
我添加了我的解决方案。 - Shweta Chauhan
尝试一下这个,简短完美的代码stackoverflow.com/a/52695444/4997704 - Binesh Kumar
显示剩余3条评论
10个回答

19

我在Nexus6p的Android N上解决了这个问题,您需要授予URI权限,以便系统相机可以访问文件,暂时等待裁剪,因为严格模式Android N不支持在Intent extra中传递文件:Uri了。详情请参阅N开发者预览版中的Scheme Ban,我们改用FileProvider。 这是我的源代码:

AndroidManifest.xml

<provider
    android:name="android.support.v4.content.FileProvider"
        android:authorities="dreamgo.corp.provider"
        android:grantUriPermissions="true"
        android:exported="false">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/filepaths"/>
</provider>

filepaths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="images" path="."/>
</paths>

MainActivity.java

Uri photoURI = FileProvider.getUriForFile(context, "dreamgo.corp.provider", file);
//grant uri with essential permission the first arg is the The packagename you would like to allow to access the Uri.
context.grantUriPermission("com.android.camera",photoURI,
                    Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);

Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(photoURI, "image/*");

//you must setup two line below
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 200);
intent.putExtra("return-data", true);
//you must setup this
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(intent, 1);

1
请问您能否提供一些建议,关于在将图片裁剪后(requestCode == CROP_IMAGE)如何存储到 ImageView 中?因为在 Android N 系统中,当我将图片裁剪后,它会在相机中崩溃。 - Shweta Chauhan
在Android N之前,我使用Bitmap bitmap = data.getParcelableExtra("data");,但在Android N上无法正常工作。我为相机创建一个临时文件,并从该临时文件获取URI,用户拍照后裁剪(剪切)临时文件。系统将在用户裁剪后更改临时文件。我将在此问题的另一个答案下发布所有代码。 - Ykh
1
这里的文件名和上下文是什么? - R.Anandan
抱歉,我无法帮助您解决此问题。 - Ykh

15
我已经找到解决方案。发布我的答案。

在MainActivity.java中
public class MainActivity extends AppCompatActivity {

@BindView(R.id.img_camera)
CircleImageView mImgCamera;

private ChoosePhoto choosePhoto=null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);
}

@OnClick(R.id.img_camera)
public void onViewClicked() {
    choosePhoto = new ChoosePhoto(this);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == ChoosePhoto.CHOOSE_PHOTO_INTENT) {
            if (data != null && data.getData() != null) {
                choosePhoto.handleGalleryResult(data);
            } else {
                choosePhoto.handleCameraResult(choosePhoto.getCameraUri());
            }
        }else if (requestCode == ChoosePhoto.SELECTED_IMG_CROP) {
            mImgCamera.setImageURI(choosePhoto.getCropImageUrl());
        }
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == ChoosePhoto.SELECT_PICTURE_CAMERA) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
            choosePhoto.showAlertDialog();
    }
}
}

ChoosePhoto.java

public class ChoosePhoto {

public static int CHOOSE_PHOTO_INTENT = 101;
public static int SELECTED_IMG_CROP = 102;
public static int SELECT_PICTURE_CAMERA = 103;
public static int currentAndroidDeviceVersion = Build.VERSION.SDK_INT;

private int ASPECT_X = 1;
private int ASPECT_Y = 1;
private int OUT_PUT_X = 300;
private int OUT_PUT_Y = 300;
private boolean SCALE = true;

private Uri cropPictureUrl, selectedImageUri = null, cameraUrl = null;
private Context mContext;

public ChoosePhoto(Context context) {
    mContext = context;
    init();
}

private void init() {
    PermissionUtil permissionUtil = new PermissionUtil();

    if (permissionUtil.checkMarshMellowPermission()) {
        if (permissionUtil.verifyPermissions(mContext, permissionUtil.getCameraPermissions()) && permissionUtil.verifyPermissions(mContext, permissionUtil.getGalleryPermissions()))
            showAlertDialog();
        else {
            ActivityCompat.requestPermissions((Activity) mContext, permissionUtil.getCameraPermissions(), SELECT_PICTURE_CAMERA);
        }
    } else {
        showAlertDialog();
    }
}

public void showAlertDialog() {
    final Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    galleryIntent.setType("image/*");

    cameraUrl = FileUtil.getInstance(mContext).createImageUri();
    //Create any other intents you want
    final Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    cameraIntent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, cameraUrl);


    //Add them to an intent array
    Intent[] intents = new Intent[]{cameraIntent};

    //Create a choose from your first intent then pass in the intent array
    final Intent chooserIntent = Intent.createChooser(galleryIntent, mContext.getString(R.string.choose_photo_title));
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intents);

    ((Activity) mContext).startActivityForResult(chooserIntent, CHOOSE_PHOTO_INTENT);
}

// Change this method(edited)
public void handleGalleryResult(Intent data) {
    try {
        cropPictureUrl = Uri.fromFile(FileUtil.getInstance(mContext)
                .createImageTempFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)));
        String realPathFromURI = FileUtil.getRealPathFromURI(mContext, data.getData());
        File file = new File(realPathFromURI == null ? getImageUrlWithAuthority(mContext, data.getData()) : realPathFromURI);
        if (file.exists()) {
            if (currentAndroidDeviceVersion > 23) {
                cropImage(FileProvider.getUriForFile(mContext, mContext.getApplicationContext().getPackageName() + ".provider", file), cropPictureUrl);
                
            } else {
                cropImage(Uri.fromFile(file), cropPictureUrl);
            }

        } else {
            cropImage(data.getData(), cropPictureUrl);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static String getImageUrlWithAuthority(Context context, Uri uri) {
    InputStream is = null;
    if (uri.getAuthority() != null) {
        try {
            is = context.getContentResolver().openInputStream(uri);
            Bitmap bmp = BitmapFactory.decodeStream(is);
            return writeToTempImageAndGetPathUri(context, bmp).toString();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}

public static Uri writeToTempImageAndGetPathUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
}


public void handleCameraResult(Uri cameraPictureUrl) {
    try {
        cropPictureUrl = Uri.fromFile(FileUtil.getInstance(mContext)
                .createImageTempFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)));

        cropImage(cameraPictureUrl, cropPictureUrl);
    } catch (IOException e) {
        e.printStackTrace();

    }

}

public Uri getCameraUri() {
    return cameraUrl;
}

public Uri getCropImageUrl() {
    return selectedImageUri;
}

private void cropImage(final Uri sourceImage, Uri destinationImage) {
    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

    intent.setType("image/*");

    List<ResolveInfo> list = mContext.getPackageManager().queryIntentActivities(intent, 0);
    int size = list.size();
    if (size == 0) {
        //Utils.showToast(mContext, mContext.getString(R.string.error_cant_select_cropping_app));
        selectedImageUri = sourceImage;
        intent.putExtra(MediaStore.EXTRA_OUTPUT, sourceImage);
        ((Activity) mContext).startActivityForResult(intent, SELECTED_IMG_CROP);
        return;
    } else {
        intent.setDataAndType(sourceImage, "image/*");
        intent.putExtra("aspectX", ASPECT_X);
        intent.putExtra("aspectY", ASPECT_Y);
        intent.putExtra("outputY", OUT_PUT_Y);
        intent.putExtra("outputX", OUT_PUT_X);
        intent.putExtra("scale", SCALE);

        //intent.putExtra("return-data", true);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, destinationImage);
        selectedImageUri = destinationImage;
        if (size == 1) {
            Intent i = new Intent(intent);
            ResolveInfo res = list.get(0);
            i.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
            ((Activity) mContext).startActivityForResult(intent, SELECTED_IMG_CROP);
        } else {
            Intent i = new Intent(intent);
            i.putExtra(Intent.EXTRA_INITIAL_INTENTS, list.toArray(new Parcelable[list.size()]));
            ((Activity) mContext).startActivityForResult(intent, SELECTED_IMG_CROP);
        }
    }
}
}

FileUtil.java

public class FileUtil {
private static FileUtil sSingleton;
private Context context;

private FileUtil(Context ctx) {
    context = ctx;
}

/**
 * Gets instance.
 *
 * @param ctx the ctx
 * @return the instance
 */
public static FileUtil getInstance(Context ctx) {
    if (sSingleton == null) {
        synchronized (FileUtil.class) {
            sSingleton = new FileUtil(ctx);
        }
    }
    return sSingleton;
}

public Uri createImageUri() {
    ContentResolver contentResolver = context.getContentResolver();
    ContentValues cv = new ContentValues();
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
    cv.put(MediaStore.Images.Media.TITLE, timeStamp);
    return contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cv);
}

/**
 * Create image temp file file.
 *
 * @param filePathDir the file path dir
 * @return the file
 * @throws IOException the io exception
 */
@SuppressLint("SimpleDateFormat")
public File createImageTempFile(File filePathDir) throws IOException {
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

    String imageFileName = "JPEG_" + timeStamp + "_";
    return File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            filePathDir      /* directory */
    );
}

public static  String getUploadFileName() {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss", Locale.US);
    Date date = new Date();
    return String.format("profile_%s.png", sdf.format(date));
}

 //add this code(edited)
 //get Path
  @TargetApi(Build.VERSION_CODES.KITKAT)
  public static String getRealPathFromURI(Context context, final Uri uri) {
    final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;

    // DocumentProvider
    if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
        // ExternalStorageProvider
        if (isExternalStorageDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            if ("primary".equalsIgnoreCase(type)) {
                return Environment.getExternalStorageDirectory() + "/" + split[1];
            }
        }
        // DownloadsProvider
        else if (isDownloadsDocument(uri)) {
            final String id = DocumentsContract.getDocumentId(uri);
            final Uri contentUri = ContentUris.withAppendedId(
                    Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));

            return getDataColumn(context, contentUri, null, null);
        }
        // MediaProvider
        else if (isMediaDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            Uri contentUri = null;
            if ("image".equals(type)) {
                contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            } else if ("video".equals(type)) {
                contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
            } else if ("audio".equals(type)) {
                contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            }

            final String selection = "_id=?";
            final String[] selectionArgs = new String[]{
                    split[1]
            };

            return getDataColumn(context, contentUri, selection, selectionArgs);
        }
    }
    // MediaStore (and general)
    else if ("content".equalsIgnoreCase(uri.getScheme())) {
        // Return the remote address
        if (isGooglePhotosUri(uri))
            return uri.getLastPathSegment();

        return getDataColumn(context, uri, null, null);
    }
    // File
    else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    } else
        return getRealPathFromURIDB(uri);

    return null;
}

/**
 * Gets real path from uri.
 *
 * @param contentUri the content uri
 * @return the real path from uri
 */
private static String getRealPathFromURIDB(Uri contentUri) {
    Cursor cursor = context.getContentResolver().query(contentUri, null, null, null, null);
    if (cursor == null) {
        return contentUri.getPath();
    } else {
        cursor.moveToFirst();
        int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
        String realPath = cursor.getString(index);
        cursor.close();
        return realPath;
    }
}

/**
 * Gets data column.
 *
 * @param uri           the uri
 * @param selection     the selection
 * @param selectionArgs the selection args
 * @return the data column
 */
public static String getDataColumn(Context context, Uri uri, String selection,
                                   String[] selectionArgs) {
    Cursor cursor = null;
    final String column = "_data";
    final String[] projection = {
            column
    };

    try {
        cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
                null);
        if (cursor != null && cursor.moveToFirst()) {
            final int index = cursor.getColumnIndexOrThrow(column);
            return cursor.getString(index);
        }
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return null;
}

/**
 * Is external storage document boolean.
 *
 * @param uri The Uri to check.
 * @return Whether the Uri authority is ExternalStorageProvider.
 */
public static boolean isExternalStorageDocument(Uri uri) {
    return "com.android.externalstorage.documents".equals(uri.getAuthority());
}

/**
 * Is downloads document boolean.
 *
 * @param uri The Uri to check.
 * @return Whether the Uri authority is DownloadsProvider.
 */
public static boolean isDownloadsDocument(Uri uri) {
    return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}

/**
 * Is media document boolean.
 *
 * @param uri The Uri to check.
 * @return Whether the Uri authority is MediaProvider.
 */
public static boolean isMediaDocument(Uri uri) {
    return "com.android.providers.media.documents".equals(uri.getAuthority());
}

/**
 * Is google photos uri boolean.
 *
 * @param uri The Uri to check.
 * @return Whether the Uri authority is Google Photos.
 */
public static boolean isGooglePhotosUri(Uri uri) {
    return "com.google.android.apps.photos.content".equals(uri.getAuthority());
}
  }

PermissionUtil.java

public class PermissionUtil {
      private String[] galleryPermissions = {
          "android.permission.WRITE_EXTERNAL_STORAGE",
          "android.permission.READ_EXTERNAL_STORAGE"
      };

private String[] cameraPermissions = {
        "android.permission.CAMERA",
        "android.permission.WRITE_EXTERNAL_STORAGE",
        "android.permission.READ_EXTERNAL_STORAGE"
};

public String[] getGalleryPermissions(){
    return galleryPermissions;
}

public String[] getCameraPermissions() {
    return cameraPermissions;
}

public boolean verifyPermissions(Context context, String[] grantResults) {
    for (String result : grantResults) {
        if (ActivityCompat.checkSelfPermission(context, result) != PackageManager.PERMISSION_GRANTED) {
            return false;
        }
    }
    return true;
}

public boolean checkMarshMellowPermission(){
    return(Build.VERSION.SDK_INT> Build.VERSION_CODES.LOLLIPOP_MR1);
}

public static void showPermissionDialog(Context mContext,String msg){
    AlertDialog.Builder builder = new AlertDialog.Builder(mContext, R.style.DatePicker);
    builder.setTitle("Need Permission");
    builder.setMessage(msg);
    builder.setPositiveButton(mContext.getString(R.string.invitation_yes), (dialogInterface, i) -> {
        dialogInterface.dismiss();
        Intent intent = new Intent();
        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        Uri uri = Uri.fromParts("package", mContext.getPackageName(), null);
        intent.setData(uri);
        (mContext).startActivity(intent);
    });

    builder.setNegativeButton(mContext.getString(R.string.invitation_del_no), (dialogInterface, i) -> {
        dialogInterface.dismiss();
    });
    builder.show();
}

提供路径的 provide_paths.xml 文件。

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
</paths>

你做得很好,但是你能告诉我如何移除裁剪区域并获得完整的图像吗? - humayoon siddique
@Shweta 你能否在这里发布 PermissionUtil 文件? - pb123
@Alex.Marinovskiy:我添加了带路径的XML文件。请检查一下。 - Shweta Chauhan
@ZeeshanGhazanfar:我更新了我的答案。有两个文件进行了两处更改,我在那里加了注释“已编辑”。 - Shweta Chauhan
1
@KrishnakantDalal:我在Android 7上也遇到了同样的错误,然后我添加了这个答案。现在,我还需要检查Android 8.0。解决问题后会通知您。 - Shweta Chauhan
显示剩余9条评论

2

我有两个来��可以裁剪,一个是相册,另一个是相机

相册的方法:

//take a photo from gallery
public void gallery() {
    //set UUID to filename
    String PHOTO_FILE_NAME = UUID.randomUUID().toString()+".jpg";
    Utils.putValue(this, Constants.UserPortraitFilePath,PHOTO_FILE_NAME);
    Intent intent = new Intent(Intent.ACTION_PICK);
    intent.setType("image/*");
    startActivityForResult(intent, PHOTO_REQUEST_GALLERY);
}

相机方法:

    //take a photo from camera
public void camera() {
    //check sdcard is usable or not
    if (Utils.hasSdcard()) {
        //set UUID to filename
        String PHOTO_FILE_NAME = UUID.randomUUID().toString()+".jpg";
        Utils.putValue(this,Constants.UserPortraitFilePath,PHOTO_FILE_NAME);
        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        //set file location to DreamGo/Image
        File path = Environment.getExternalStorageDirectory();
        File dir = new File(path, "DreamGo/Image");
        if(!dir.exists())
            dir.mkdirs();
        //Android N need use FileProvider get file 
        //uri because StrictMode System
        //getUriForFile(content,provider author,file)
        Uri photoURI = FileProvider.getUriForFile(context, "dream.go.provider",
                new File(dir.getAbsolutePath(), PHOTO_FILE_NAME));
        intent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
        startActivityForResult(intent, PHOTO_REQUEST_CAMERA);
    }else {
        showToast("no storage device");
    }
}

crop方法:

    //Android N crop image
public void crop(Uri uri) {
    context.grantUriPermission("com.android.camera",uri,
            Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setDataAndType(uri, "image/*");
    //Android N need set permission to uri otherwise system camera don't has permission to access file wait crop
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    intent.putExtra("crop", "true");
    //The proportion of the crop box is 1:1
    intent.putExtra("aspectX", 1);
    intent.putExtra("aspectY", 1);
    //Crop the output image size
    intent.putExtra("outputX", 800);
    intent.putExtra("outputY", 800);
    //image type
    intent.putExtra("outputFormat", "JPEG");
    intent.putExtra("noFaceDetection", true);
    //true - don't return uri |  false - return uri
    intent.putExtra("return-data", true);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
    startActivityForResult(intent, PHOTO_REQUEST_CUT);
}

onActivityResult 方法:

private static final int PHOTO_REQUEST_CAMERA = 0;//camera
private static final int PHOTO_REQUEST_GALLERY = 1;//gallery
private static final int PHOTO_REQUEST_CUT = 2;//image crop
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    String PHOTO_FILE_NAME = Utils.getValue(this, Constants.UserPortraitFilePath);
    File path = Environment.getExternalStorageDirectory();
    File dir = new File(path, "DreamGo/Image");
    if(!dir.exists())
        dir.mkdirs();
    switch (requestCode)
    {
        case PHOTO_REQUEST_GALLERY:
            if (data != null){
                //file from gallery
                File sourceFile = new File(getRealPathFromURI(data.getData()));
                //blank file DreamGo/Image/uuid.jpg
                File destFile = new File(dir.getAbsolutePath(), PHOTO_FILE_NAME);
                Log.e("photo",data.getData().getPath());
                try {
                    //copy file from gallery to DreamGo/Image/uuid.jpg
                    // otherwise crop method can't cut image without write permission
                    copyFile(sourceFile,destFile);
                    //Android N need use FileProvider to get file uri
                    Uri photoURI = FileProvider.getUriForFile(context, "dream.go.provider", destFile);
                    //cut image
                    crop(photoURI);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            break;
        case PHOTO_REQUEST_CAMERA:
            //whether sdcard is usable has been checked before use camera
            File tempFile = new File(dir.getAbsolutePath(), PHOTO_FILE_NAME);
            Uri photoURI = FileProvider.getUriForFile(context, "dream.go.provider", tempFile);
            crop(photoURI);
            break;
        case PHOTO_REQUEST_CUT:
            try {
                if(data!=null) {
                    file = new File(dir.getAbsolutePath(), PHOTO_FILE_NAME);
                    icon.loadImage("file://" + file.getAbsolutePath());
                }else {
                    showToast("a error happened when cut picture");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            break;
        default:
            break;
    }
}

相关代码:

//copy sourceFile to destFile
public void copyFile(File sourceFile, File destFile) throws IOException {
    if (!sourceFile.exists()) {
        return;
    }
    FileChannel source = new FileInputStream(sourceFile).getChannel();
    FileChannel destination = new FileOutputStream(destFile).getChannel();
    if (destination != null && source != null) {
        destination.transferFrom(source, 0, source.size());
    }
    if (source != null) {
        source.close();
    }
    if (destination != null) {
        destination.close();
    }
}

//file uri to real location in filesystem
public String getRealPathFromURI(Uri contentURI) {
    Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
    if (cursor == null) {
        // Source is Dropbox or other similar local file path
        return contentURI.getPath();
    } else {
        cursor.moveToFirst();
        int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
        return cursor.getString(idx);
    }
}

public static final String getValue(Context context, String key) {
    return getSharedPreference(context).getString(key, "");
}
public static final boolean putValue(Context context, String key,
                                     String value) {
    value = value == null ? "" : value;
    SharedPreferences.Editor editor = getSharedPreference(context).edit();
    editor.putString(key, value);
    boolean result = editor.commit();
    if (!result) {
        return false;
    }
    return true;
}

1
Uri photoURI = FileProvider.getUriForFile(context, "dream.go.provider", destFile); 在这行代码中,我遇到了文件未找到的异常。 - Atul Dhanuka

1
你在 Manifest.xml 中添加了这个吗?
<application
        ........
    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>
</application>

这必须在你的清单中存在...对于在Android 7.0上产生影响。

同样,您需要在xml中添加provider_paths.xml文件。

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
</paths>

1
请按照此教程操作:https://inthecheesefactory.com/blog/how-to-share-access-to-file-with-fileprovider-on-android-nougat/en - Arpit Patel
我参考了这个教程,并从中得到了这个解决方案(Provider)。我已经跟着这个教程完成了相机裁剪的部分,但在裁剪后它崩溃了。 - Shweta Chauhan

1
    Uri uri = FileProvider.getUriForFile(this, getPackageName() + Configs.FILE_PROVIDER_NAME, inFile);
    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    intent.setDataAndType(uri, "image/*");
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(outFile));

请注意,EXTRA_OUTPUT的URI不应该被FileProvider修改。您的paths.xml应该像这样。
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_sd" path="."/>
<external-files-path name="external_app" path="."/>
<files-path name="files" path="."/>
<cache-path name="cache" path="."/>

因为你在getExternalFilesDir下创建了裁剪文件,所以<external-files-path>是必要的。

1

以下解决方案对我有效。我已经测试过了,包括Gallery、Google Drive、Photos等。

示例使用Kotlin语言编写。

ImagePickUtils.kt

fun getImageUri(context: Context, contentURI: String): Uri {
    var conUri = Uri.parse(contentURI)
    var filePath = ""
    if (DocumentsContract.isDocumentUri(context, conUri)) {
        val wholeID = DocumentsContract.getDocumentId(conUri)

        // Split at colon, use second item in the array
        val id = wholeID.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()[1]

        val column = arrayOf(MediaStore.Images.Media.DATA)

        // where id is equal to
        val sel = MediaStore.Images.Media._ID + "=?"

        val cursor = context.contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                column, sel, arrayOf(id), null) ?: return conUri

        val columnIndex = cursor.getColumnIndex(column[0])

        if (cursor.moveToFirst()) {
            filePath = cursor.getString(columnIndex)
        }
        cursor.close()

        if (filePath.isNotEmpty()) {
            filePath = filePath.replace(" ".toRegex(), "%20")
            conUri = Uri.parse("file://$filePath")
        }
    }
    return conUri
}

onActivityResult是Activity / Fragment中的一个方法:

if (data != null) {
    val imagePath: Uri
    if (data.data != null) {
           val mImageUri = data.data
           imagePath = getImageUri(this@HomeActivity, mImageUri.toString())
           Log.i(TAG+" Image actual path", imagePath.toString())
    }
}

希望这能对您有所帮助。

0

您可以轻松使用这个,它将有助于清除您的项目中的样板代码,以进行图像选择和裁剪。


0
Intent intent = new Intent("com.android.camera.action.CROP"); 

在谷歌相册中,这种裁剪方法无法正常工作,我意识到并不是所有设备和图库都支持它,因此我创建了自己的裁剪方法。目前我仍然面临两个小问题,如果有人能帮我解决,那就太好了。否则,这个类还是可以正常工作的。

可绘制对象:

crop_rectangle.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">

<stroke android:color="@color/white"
    android:width="3dp"
    android:dashGap="10dp"
    android:dashWidth="10dp"/>
</shape>

Layout: activity_crop.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black">

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginVertical="70dp">

    <FrameLayout

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:id="@+id/full_screen_image_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:scaleType="centerCrop"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:tint="#aa111111" />

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/image_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:id="@+id/crop_area"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_centerInParent="true"
                android:adjustViewBounds="true"
                android:background="@drawable/crop_rectangle"
                android:scaleType="fitCenter"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

        </androidx.constraintlayout.widget.ConstraintLayout>

    </FrameLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

<ImageView
    android:id="@+id/close_image_viewer"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:layout_margin="20dp"
    android:padding="5dp"
    android:src="@drawable/ic_close"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:tint="@color/white" />

<ImageView
    android:id="@+id/save_image"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:layout_margin="20dp"
    android:padding="5dp"
    android:scaleType="fitCenter"
    android:src="@drawable/ic_tick"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:tint="@color/white" />

</androidx.constraintlayout.widget.ConstraintLayout>

CropActivity.kt

class CropActivity : Activity() {

private lateinit var closeBt: ImageView
private lateinit var imageView: ImageView
private lateinit var cropArea: ImageView
private lateinit var saveImage: ImageView

private var mScaleGestureDetector: ScaleGestureDetector? = null
private var mGestureListener: GestureDetector? = null
var mScaleFactor = 1.0f
var xPos = 0.0f
var yPos = 0.0f
val minZoom = 0.2f
val maxZoom = 1.0f

lateinit var bmp: Bitmap

var oldE2X: Float = 0f
var oldE2Y: Float = 0f
var height: Int = 0
var width: Int = 0

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_crop)
    closeBt = findViewById(R.id.close_image_viewer)
    imageView = findViewById(R.id.full_screen_image_view)
    cropArea = findViewById(R.id.crop_area)
    saveImage = findViewById(R.id.save_image)

    val displayMetrics = DisplayMetrics()
    windowManager.defaultDisplay.getMetrics(displayMetrics)
    height = displayMetrics.heightPixels
    width = displayMetrics.widthPixels

    val filepath = intent.getStringExtra(AppConsts.IMAGE_STRING)
    try {
        //val file = File(filepath!!)
        bmp = getResizedBitmap(BitmapFactory.decodeFile(filepath), width)!!
        imageView.setImageBitmap(bmp)

        val layoutParams = cropArea.layoutParams as ConstraintLayout.LayoutParams
        val ratio = if (intent.hasExtra(AppConsts.IMAGE_RATIO))
            intent.getStringExtra(AppConsts.IMAGE_RATIO)
        else
            "H,1:1"

        layoutParams.dimensionRatio = ratio

        val imageWidth = bmp.width
        val imageHeight = bmp.height
        val bitmapRatio: Float = imageWidth.toFloat() / imageHeight.toFloat()

        cropArea.layoutParams = layoutParams

        if (bitmapRatio <= 1f) {
            cropArea.layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT
        } else {
            cropArea.layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT
        }

    } catch (e: Exception) {
        e.printStackTrace()
        Toast.makeText(this, e.message, Toast.LENGTH_LONG).show()
        finish()
    }

    xPos = cropArea.x
    yPos = cropArea.y
    mScaleGestureDetector = ScaleGestureDetector(this, ScaleListener())
    mGestureListener = GestureDetector(this, GestureListener())

    saveImage.setOnClickListener {
        saveImage()
    }

}

private fun saveImage() {
    val loc = IntArray(2)
    cropArea.getLocationOnScreen(loc)

    val locImage = IntArray(2)
    imageView.getLocationOnScreen(locImage)

    var bx = loc[0] - locImage[0]
    var by = loc[1] - locImage[1]
    val bWidth = (cropArea.width * mScaleFactor).toInt()
    val bHeight = (cropArea.height * mScaleFactor).toInt()

    if (bx + bWidth > bmp.width)
        bx = getCorrectBx(bx, bWidth)

    if (bx < 0)
        bx = 0

    if (by + bHeight > bmp.height)
        by = getCorrectYx(by, bHeight)

    if (by < 0)
        by = 0

    val croppedBmp: Bitmap = Bitmap.createBitmap(
        bmp,
        bx,
        by,
        bWidth,
        bHeight
    )

    val dir = getExternalFilesDir(Environment.DIRECTORY_PICTURES)?.absolutePath
    val file = File(dir!!)
    if (!file.exists())
        file.mkdirs()

    val path = "$dir/cameraResult.png"
    val image = File(path)
    if (!image.exists())
        image.createNewFile()

    val byteArrayOutputStream = ByteArrayOutputStream()
    croppedBmp.compress(Bitmap.CompressFormat.PNG, 30, byteArrayOutputStream)
    val byteArray: ByteArray = byteArrayOutputStream.toByteArray()
    image.writeBytes(byteArray)

    val data = Intent()
    data.putExtra(AppConsts.FILE_URI, Uri.fromFile(image).toString())
    setResult(RESULT_OK, data)
    finish()
    //imageView.setImageBitmap(croppedBmp)
}

private fun getCorrectBx(bx: Int, bWidth: Int): Int {
    val newbx = bx - (bmp.width - (bx + bWidth) + 10)
    return if (newbx + bWidth > bmp.width)
        getCorrectBx(newbx, bWidth)
    else
        newbx
}

private fun getCorrectYx(by: Int, bHeight: Int): Int {
    val newby = by - (bmp.height - (by + bHeight) + 10)
    return if (newby + bHeight > bmp.height)
        getCorrectBx(newby, bHeight)
    else
        newby
}

override fun onTouchEvent(motionEvent: MotionEvent?): Boolean {
    super.onTouchEvent(motionEvent)
    mScaleGestureDetector!!.onTouchEvent(motionEvent)
    mGestureListener!!.onTouchEvent(motionEvent)
    return true
}

inner class ScaleListener : ScaleGestureDetector.SimpleOnScaleGestureListener() {
    override fun onScale(scaleGestureDetector: ScaleGestureDetector): Boolean {
        mScaleFactor *= scaleGestureDetector.scaleFactor
        mScaleFactor = minZoom.coerceAtLeast(mScaleFactor.coerceAtMost(maxZoom))
        cropArea.scaleX = mScaleFactor
        cropArea.scaleY = mScaleFactor
        val loc = IntArray(2)
        cropArea.getLocationOnScreen(loc)
        return true
    }
}

inner class GestureListener : GestureDetector.SimpleOnGestureListener() {
    override fun onScroll(
        e1: MotionEvent?,
        e2: MotionEvent?,
        distanceX: Float,
        distanceY: Float
    ): Boolean {
        val oldCropLoc = IntArray(2)
        cropArea.getLocationOnScreen(oldCropLoc)

        val locImage = IntArray(2)
        imageView.getLocationOnScreen(locImage)

        if (oldE2X - e2!!.x > 100f || oldE2X - e2.x < -100f) {
            oldE2X = e2.x
            return true
        }

        if (oldE2Y - e2.y > 100f || oldE2Y - e2.y < -100f) {
            oldE2Y = e2.y
            return true
        }

        when {
            oldCropLoc[0] < locImage[0] + 10f && (oldE2X - e2.x) > 0f -> {
                //cropArea.translationX = (-oldCropLoc[0]).toFloat()
                //cropArea.left = imageView.left + 1
                oldE2X = width.toFloat()
            }

            (oldCropLoc[0] + cropArea.width * mScaleFactor) + 10f > locImage[0] + imageView.width &&
                    (oldE2X - e2.x) < 0f -> {
                //cropArea.right = imageView.right - 1
                oldE2X = 0f
            }

            else -> {
                xPos += distanceX
                cropArea.translationX = -xPos
            }
        }

        when {
            oldCropLoc[1] < locImage[1] + 10f && (oldE2Y - e2!!.y) > 0f -> {
                //cropArea.top = imageView.top + 1
                oldE2Y = height.toFloat()
            }

            (oldCropLoc[1] + cropArea.height * mScaleFactor) + 10f > locImage[1] + imageView.height &&
                    (oldE2Y - e2.y) < 0f -> {
                oldE2Y = 0f
                //cropArea.bottom = imageView.bottom - 1
            }

            else -> {
                yPos += distanceY
                cropArea.translationY = -yPos
            }
        }
        oldE2X = e2.x
        oldE2Y = e2.y
        return true
    }
}

private fun highlightPixels(myBitmap: Bitmap/*, a: Int, b: Int, c: Int, d: Int*/){
    val pixels = IntArray(myBitmap.height * myBitmap.width)
    myBitmap.getPixels(pixels, 0, myBitmap.width, 0, 0, myBitmap.width, myBitmap.height)
    for (i in 0 until myBitmap.width * 5) pixels[i] = Color.BLUE
    myBitmap.setPixels(pixels, 0, myBitmap.width, 0, 0, myBitmap.width, myBitmap.height)
}

private fun getResizedBitmap(image: Bitmap, maxSize: Int): Bitmap? {
    var width = image.width
    var height = image.height
    val bitmapRatio = width.toFloat() / height.toFloat()
    width = maxSize
    height = (width / bitmapRatio).toInt()
    return Bitmap.createScaledBitmap(image, width, height, true)
}
}

如何使用:
从您的图像选择活动中假设您正在从图像选择器中获得适当的 Uri。将该 Uri 传递给以下函数:
private fun openCropActivity(contentUri: Uri) {
    val bitmap = MediaStore.Images.Media.getBitmap(
        contentResolver, contentUri
    )

    val dir = getExternalFilesDir(Environment.DIRECTORY_PICTURES)?.absolutePath
    val file = File(dir!!)
    if (!file.exists())
        file.mkdirs()

    val path = "$dir/cameraResult.png"
    val image = File(path)
    if (!image.exists())
        image.createNewFile()

    val byteArrayOutputStream = ByteArrayOutputStream()
    bitmap.compress(Bitmap.CompressFormat.PNG, 30, byteArrayOutputStream)
    val byteArray: ByteArray = byteArrayOutputStream.toByteArray()
    image.writeBytes(byteArray)

    val cropIntent = Intent(this, CropActivity::class.java)
    cropIntent.putExtra(AppConsts.IMAGE_STRING, path)
    startActivityForResult(cropIntent, RESULT_CROP)
}

在您的活动结果中,获取裁剪图像的URI如下:

if (resultCode == RESULT_OK && data != null) {
    val uri: Uri? = Uri.parse(data.getStringExtra(AppConsts.FILE_URI))
}

根据您的意愿分配常量。

我面临的两个问题是:

  1. 纵横比仍然固定,如果没有,则从意图或1:1中获取。您无法使用手势或任何内容更改纵横比

  2. 我想突出未裁剪/裁剪区域,以便在背景为白色时可以轻松区分这些区域。


0

这对我来说非常有效,适用于Marshmallow和7.1.2(Naugat)

  1. 要从相机中选择图像,请按照this answer中所述的方法,从this博客中按照以下步骤操作:

按照此步骤编写代码,适用于Marshmallow和7.1.2(Naugat)

  1. 如果您想使用意图com.android.camera.action.CROP(实际上不建议使用,但仍有许多开发人员使用),则可以使用旧样式中的URI调用这些方法。即Uri.fromFile(file)

cropIntent.setDataAndType(Uri.fromFile(file), "image/*");

cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));

以及其他方法(请搜索如何调用意图com.android.camera.action.CROP

最后,在调用startActivityForResult(cropIntent, CROP_ACTIVITY_CODE)之前,请务必编写以下内容...
        if(Build.VERSION.SDK_INT>=24)
        {
            try
            {
                Method m = StrictMode.class.getMethod("disableDeathOnFileUriExposure");
                m.invoke(null);
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }

就是这样!希望能对某些人有所帮助。


0
Intent pickImageIntent = new Intent("com.android.camera.action.CROP");
Uri contentUri = imageUri;
pickImageIntent.setDataAndType(contentUri, "image/*");
pickImageIntent.putExtra("crop", "true");
pickImageIntent.putExtra("aspectX", 1);
pickImageIntent.putExtra("aspectY", 1);
pickImageIntent.putExtra("outputX", 400);
pickImageIntent.putExtra("outputY", 400);
pickImageIntent.putExtra("return-data", true);
startActivityForResult(pickImageIntent, RESULT_CROP);

2
你应该提供有关代码如何解决问题和/或为什么解决问题的额外上下文。这将提高答案的长期价值。请记住,您正在回答未来读者的问题,而不仅仅是现在提问的人!请编辑您的答案以添加解释,并指出适用的限制和假设。此外,提到为什么此答案比其他答案更合适也没有坏处。 - Dev-iL

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接