1

crop() 的输出 URI 始终为空。值得注意的是,结果代码是RESULT_CANCELLED针对requestCode == Crop.REQUEST_CROP.

onActivityResult 函数:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
       // tell mediaplayer to add pic to is database
        galleryAddPic();
        // Use mPhotopath to start cropping asynchronously
        mPhotoCropUri = null;
        Crop.of(mPhotoURI, mPhotoCropUri).asSquare().start(this);
    } else if (requestCode == Crop.REQUEST_CROP && resultCode == RESULT_OK) {
        Log.e("Crop", mPhotoCropUri.toString());
    }
}

我正在调用 dispatchTakePictureIntent() 来拍照并存储它。这是代码:

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File

        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            mPhotoURI = FileProvider.getUriForFile(this,
                    "com.example.android.fileprovider",
                    photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mPhotoURI);
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }
}

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new java.util.Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mPhotoPath = "file:" + image.getAbsolutePath();
    return image;
}

private void galleryAddPic() {
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    File f = new File(mPhotoPath);
    Uri contentUri = Uri.fromFile(f);
    mediaScanIntent.setData(contentUri);
    this.sendBroadcast(mediaScanIntent);
}

存储图片后,我将存储图片的URI传递给Android-crop库的crop函数。

4

0 回答 0