使用牛轧糖中的摄像头捕获图像,使用.fileprovider在农作物对话框中获取无效图像



这是我的代码

mainifest

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
 <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>

provider_path.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>

在这里检查naugat是否执行此

if (haswritePermission == PackageManager.PERMISSION_GRANTED && hasWriteContactsPermission == PackageManager.PERMISSION_GRANTED) {
                ++imageId;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
        String imageFilePath = Environment.getExternalStorageDirectory().toString() + "/Images/Profile/profile.jpg";
        File imageFile = new File(imageFilePath);
        imageUri = FileProvider.getUriForFile(MainActivity.this, BuildConfig.APPLICATION_ID + ".provider", imageFile);
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
        startActivityForResult(intent, PICK_FROM_CAMERA);
    } 
}

onActivityResult

public void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
               if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PICK_FROM_CAMERA:
                    mCropImageView.setImageUriAsync(imageUri);
                    [crop_dialog][1].show();
                    break;
            }
    }
}

对于作物图像,我正在使用此库

compile 'com.theartofdev.edmodo:android-image-cropper:2.5.+'

获取这种空图像或空图像

I just change code for Nougat .add separate case for nougat
**for less than M** 
 case PICK_FROM_CAMERA:
                        mCropImageView.setImageUriAsync(imageUri);
                        path=getRealPathFromURI(imageUri);                       
                        Bitmap bitmap=rotateBitmapOrientation(path);
                        mCropImageView.setImageBitmap(bitmap);
                        crop_dialog.show();
                        break;

**for Nougat ** 
case CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE:
                        mCropImageView.setImageUriAsync(imageUri);
                        selectedImage);
                        crop_dialog.show();
                        break; 

最新更新