Nougat:错误,而#34;裁剪图像并在ImageView"中显示?

时间:2018-03-23 05:17:15

标签: android android-camera-intent onactivityresult image-capture

我在Android版 Nougat 的情况下遇到以下错误,捕获图像并在imageview中显示但它在marshmallow中工作。我创建了 Fileprovider 路径牛轧糖是必需的。

下面是相机和裁剪的代码。

camera.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialogBuilder.dismiss();
                    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
                   /* File file = new File(Environment.getExternalStorageDirectory() + File.separator + "img.jpg");
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
                    startActivityForResult(intent, 100);*/
                    file = new File(UploadIdActivtiy.this.getExternalCacheDir(),
                            String.valueOf(System.currentTimeMillis()) + ".jpg");
                    if (Build.VERSION.SDK_INT >= 24) {
                        uri = FileProvider.getUriForFile(UploadIdActivtiy.this,
                                BuildConfig.APPLICATION_ID + ".provider",
                                file);
                    }else{
                        uri = Uri.fromFile(file);
                    }
                     Log.d("camerapath==", uri.getPath());
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
                    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                    startActivityForResult(intent, 100);
                }
            });

//裁剪捕获的图像

private void performCrop() {
        try {
            UploadIdActivtiy.this.grantUriPermission("com.activatic.paychek",uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);


            UploadIdActivtiy.this.revokeUriPermission(uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);

            Intent cropIntent = new Intent("com.android.camera.action.CROP");
            //indicate image type and Uri
            cropIntent.setDataAndType(uri, "image/*");

            cropIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            cropIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            //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);

            cropIntent.putExtra("outputFormat", "JPEG");
            cropIntent.putExtra("noFaceDetection", true);
            //retrieve data on return
            cropIntent.putExtra("return-data", true);

            if (Build.VERSION.SDK_INT >= 24) {

                uri = FileProvider.getUriForFile(UploadIdActivtiy.this,
                        BuildConfig.APPLICATION_ID + ".provider", createCropFile());

            } else
                uri = Uri.fromFile(createCropFile());
            //start the activity - we handle returning in onActivityResult
            cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
            startActivityForResult(cropIntent, PIC_CROP);
        } catch (ActivityNotFoundException anfe) {
            //display an error message
            String errorMessage = "Whoops - your device doesn't support the crop action!";
            Toast toast = Toast.makeText(getApplicationContext(), errorMessage, Toast.LENGTH_SHORT);
            toast.show();
        }
    }

// createcropfile function

 private File createCropFile() {
        File storageDir = UploadIdActivtiy.this.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(UploadIdActivtiy.this,
                    BuildConfig.APPLICATION_ID + ".provider", file));
        else
            mCurrentPhotoPath = String.valueOf(Uri.fromFile(file));
        return file;
    }

// OnActivtiy结果

   @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == -1) {

            if (requestCode == 100 ) {
                Log.e("Exception 100","resultCode >> "+resultCode);
                File file = new File(Environment.getExternalStorageDirectory()+File.separator + "img.jpg");

                Log.d("showimgfile","==="+file);
                Log.d("shosimg","%%=="+Uri.fromFile(file));


                try{
                    Log.d("shosimg","%%=="+file);
                    Log.d("shosimg","%%=="+Uri.fromFile(file));

                    //ImageCropFunction(Uri.fromFile(file));
//                    uri = data.getData();
                    performCrop();
                }catch (ActivityNotFoundException aNEF){
                    String errorMessage = "Sorry - your device doesn't support the crop action!";
                    Toast toast = Toast.makeText(getApplicationContext(), errorMessage, Toast.LENGTH_SHORT);
                    toast.show();
                }
            }

            else if (requestCode == 10) {
                Log.d("Exception 10", "resultCode >> " + resultCode);

                uri = data.getData();
                performCrop();
            }
            else if(requestCode == PIC_CROP){
                Log.d("imavalue==",""+img);
                if(img==200)
                {
                    Bundle extras = data.getExtras();
                    thePic_imgone = extras.getParcelable("data");

                    CheckCount_imgone = 1;
                    BitmapDrawable background = new BitmapDrawable(thePic_imgone);
                    llFront.setBackgroundDrawable(background);

                    iv_front.setVisibility(View.GONE);
                    txt_front.setVisibility(View.GONE);
                    //iv_front.setImageBitmap(thePic_imgone);

                }

                if(img==300)
                {
                    Bundle extras = data.getExtras();
                    thePic_imgtwo = extras.getParcelable("data");

                    CheckCount_imgtwo = 2;
                    BitmapDrawable background = new BitmapDrawable(thePic_imgtwo);
                    llBack.setBackgroundDrawable(background);

                    ivbackimage.setVisibility(View.GONE);
                    txt_back.setVisibility(View.GONE);
                    //ivbackimage.setImageBitmap(thePic_imgtwo);

                }
            }

        }else{
            Log.e("Exception", "resultCode >> " + resultCode);
        }
    }

//我面临的错误

   java.lang.SecurityException: Permission Denial: writing android.support.v4.content.FileProvider uri content://com.activatic.paychek.provider/external_files/Android/data/com.activatic.paychek/files/Pictures/20180323_103545290646785.jpg from pid=16668, uid=10046 requires the provider be exported, or grantUriPermission()
                                                                          at android.content.ContentProvider.enforceWritePermissionInner(ContentProvider.java:682)
                                                                          at android.content.ContentProvider$Transport.enforceWritePermission(ContentProvider.java:497)
                                                                          at android.content.ContentProvider$Transport.enforceFilePermission(ContentProvider.java:469)
                                                                          at android.content.ContentProvider$Transport.openAssetFile(ContentProvider.java:384)
                                                                          at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:262)
                                                                          at android.os.Binder.execTransact(Binder.java:565)

1 个答案:

答案 0 :(得分:0)

你在清单中添加了吗?只需在清单文件中添加它

<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">