SecurityException:权限拒绝从图库中裁剪照片

时间:2014-09-16 14:59:30

标签: android android-intent android-4.4-kitkat android-gallery

我一直在寻找这个问题,但是我找不到并回答,我发现这是KitKat上的一个问题和一些可能的解决方案,但我发现尝试裁剪从画廊中选择的图像。 我不知道如何解决它,提前感谢有人能给我的任何帮助。

当我已经获得所选的照片数据时,从onActivityResult调用裁剪方法。

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

    if (data != null) {

        switch (requestCode) {

            case CROP_FROM_CAMERA:
                Bundle extras = data.getExtras();

                mIsGallery = false;
                if (extras != null) {
                    Bitmap photo = extras.getParcelable("data");
                    extras.putString(AppConstants.PHOTO_URI, getImageUri(photo).toString());
                    extras.putParcelable(AppConstants.EVENT_DATA, mEventDetails);
                    CameraFilters cameraFilters = new CameraFilters();
                    cameraFilters.setArguments(extras);

                    mFragmentManager.beginTransaction()
                            .replace(R.id.content_frame, cameraFilters)
                            .addToBackStack(AppConstants.CUSTOM_CAMERA_FRAGMENT).commit();

                }
                break;

            case MEDIA_TYPE_GALLERY:
                mImageCaptureUri = data.getData();
                doCrop();
                break;
        }
    }
}


private void doCrop() {
    final ArrayList<CropOption> cropOptions = new ArrayList<CropOption>();

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

    List<ResolveInfo> list = getActivity().getPackageManager().queryIntentActivities(intent, 0);

    int size = list.size();

    if (size == 0) {
        String errorMessage = "Whoops - your device doesn't support the crop action!";
        Toast toast = Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_SHORT);
        toast.show();
    } else {
        intent.setData(mImageCaptureUri);

        intent.putExtra("outputX", 200);
        intent.putExtra("outputY", 200);
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("scale", true);
        intent.putExtra("return-data", true);

        if (size == 1) {
            Intent i        = new Intent(intent);
            ResolveInfo res = list.get(0);

            i.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));

            startActivityForResult(i, CROP_FROM_CAMERA);
        } else {
            for (ResolveInfo res : list) {
                final CropOption co = new CropOption();

                co.title    = getActivity().getPackageManager().getApplicationLabel(res.activityInfo.applicationInfo);
                co.icon     = getActivity().getPackageManager().getApplicationIcon(res.activityInfo.applicationInfo);
                co.appIntent= new Intent(intent);

                co.appIntent.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));

                cropOptions.add(co);
            }

            CropOptionAdapter adapter = new CropOptionAdapter(getActivity().getApplicationContext(), cropOptions);

            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setTitle("Choose Crop App");
            builder.setAdapter( adapter, new DialogInterface.OnClickListener() {
                public void onClick( DialogInterface dialog, int item ) {
                    startActivityForResult( cropOptions.get(item).appIntent, CROP_FROM_CAMERA);
                }
            });

            builder.setOnCancelListener( new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel( DialogInterface dialog ) {

                    if (mImageCaptureUri != null ) {
                        getActivity().getContentResolver().delete(mImageCaptureUri, null, null );
                        mImageCaptureUri = null;
                    }
                }
            } );

            AlertDialog alert = builder.create();

            alert.show();
        }
    }
}

Logcat异常

    09-16 09:32:27.559  22109-22130/? E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
    Process: com.motorola.MotGallery2, PID: 22109
      java.lang.RuntimeException: An error occured while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:300)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
        at java.util.concurrent.FutureTask.run(FutureTask.java:242)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at java.lang.Thread.run(Thread.java:841)
     Caused by: java.lang.SecurityException: Permission Denial: opening provider     com.android.providers.media.MediaDocumentsProvider from ProcessRecord{422e4c70 22109:com.motorola.MotGallery2/u0a30} (pid=22109, uid=10030) requires android.permission.MANAGE_DOCUMENTS or android.permission.MANAGE_DOCUMENTS
        at android.os.Parcel.readException(Parcel.java:1465)
        at android.os.Parcel.readException(Parcel.java:1419)
        at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:2887)
        at android.app.ActivityThread.acquireProvider(ActivityThread.java:4480)
        at   android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2323)
        at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1425)
        at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1047)
        at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:904)
        at android.content.ContentResolver.openInputStream(ContentResolver.java:629)
        at com.android.gallery3d.filtershow.cache.ImageLoader.loadBitmap(ImageLoader.java:331)
        at com.android.gallery3d.filtershow.cache.ImageLoader.loadBitmapBounds(ImageLoader.java:306)
        at  com.android.gallery3d.filtershow.cache.ImageLoader.loadConstrainedBitmap(ImageLoader.java:359)
        at com.android.gallery3d.filtershow.crop.CropActivity$LoadBitmapTask.doInBackground(CropActivity.java:277)
        at com.android.gallery3d.filtershow.crop.CropActivity$LoadBitmapTask.doInBackground(CropActivity.java:261)
        at android.os.AsyncTask$2.call(AsyncTask.java:288)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at java.lang.Thread.run(Thread.java:841)

这些是我在清单上的权限:

<!-- Permisions -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS"/>

0 个答案:

没有答案