android:撤销权限

Posted

技术标签:

【中文标题】android:撤销权限【英文标题】:android: revoked permission 【发布时间】:2016-06-05 00:19:18 【问题描述】:

美好的一天 我正在尝试制作捕获图像然后在gridview中显示的应用程序 但是当我单击按钮开始捕获此错误时出现。

Logcat:

java.lang.SecurityException: Permission Denial: starting Intent 
act=android.media.action.IMAGE_CAPTURE flg=0x3
cmp=com.android.camera2/com.android.camera.CaptureActivity
clip=text/uri-list
U:file:///storage/emulated/0/Pictures/MyInvoice/IMG_20160223_032401.jpg
(has extras)  from ProcessRecord8f9b88f
1356:com.example.labon.invoicemanger/u0a62 (pid=1356, uid=10062) with
revoked permission android.permission.CAMERA
at android.os.Parcel.readException(Parcel.java:1599)

我的代码:

    myLists = new ArrayList<Images>();
    adapter = new ImageListAdapter(getActivity(), R.layout.img_list_view, myLists);
    Button myButton = (Button) view.findViewById(R.id.camerabutton);
    myButton.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);// create a file to save the image
            intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
            startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE); // start the image capture Intent
        
    );
    myGridView = (GridView) view.findViewById(R.id.gridView);
    myGridView.setAdapter(adapter);   /**
 * Create a file Uri for saving an image or video
 */
public Uri getOutputMediaFileUri(int type) 

    return Uri.fromFile(getOutputMediaFile(type));

/**
 * returning image /
 */
private static File getOutputMediaFile(int type) 

    // External sdcard location
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), IMAGE_DIRECTORY_NAME);

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) 
        mediaStorageDir.mkdirs();
        // if (!mediaStorageDir.mkdirs()) 
        Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
                + IMAGE_DIRECTORY_NAME + " directory");
        //return null;

    

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
            Locale.getDefault()).format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE) 
        mediaFile = new File(mediaStorageDir.getPath() + File.separator
                + "IMG_" + timeStamp + ".jpg");
     else 
        return null;
    


    return mediaFile;





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


    switch (requestCode) 
        case 1:
            if (resultCode == Activity.RESULT_OK) 
                Uri selectedImage = data.getData();
                String[] filePathColumn = MediaStore.Images.Media.DATA;
                Cursor cursor = getActivity().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                //file path of captured image
                String filePath = cursor.getString(columnIndex);
                //file path of captured image
                File f = new File(filePath);
                String filename = f.getName();
                cursor.close();

                //Convert file path into bitmap image using below line.
                Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
                //put bitmapimage in your imageview
                ImageView imageView = (ImageView) view.findViewById(R.id.imageListView);
                imageView.setImageBitmap(BitmapFactory.decodeByteArray(images.getImageBlob(), 0, images.getImageBlob().length));
                //newImageView.setImageBitmap(yourSelectedImage);  
    if (resultCode == getActivity().RESULT_CANCELED) 
        // user cancelled Image capture
        Toast.makeText(getActivity(), "User cancelled image capture", Toast.LENGTH_SHORT)
                .show();
     else 
        // failed to capture image
        Toast.makeText(getActivity(), "Sorry! Failed to capture image", Toast.LENGTH_SHORT)
                .show();
    
 

【问题讨论】:

请参考***.com/questions/32789027/… 你是否在清单中添加了权限 是的,我已经添加了它们,但仍然是同样的问题@sankyjain 你是在 marshmellow 上运行这个吗? 【参考方案1】:

你是在棉花糖上运行这个吗?因为如果你是,权限结构已经改变。你应该阅读文档@http://developer.android.com/training/permissions/index.html

用户必须明确授予权限。

【讨论】:

【参考方案2】:

你需要给androidmanifest.xml添加用户权限

<uses-permission android:name="android.permission.CAMERA" />

如果您需要将图像存储到存储中,则必须添加:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

【讨论】:

以上是关于android:撤销权限的主要内容,如果未能解决你的问题,请参考以下文章

解决错误::撤销权限android.permission.CALL_PHONE

Android M:以编程方式撤销权限

Android M:以编程方式撤销权限

如何在定位服务启动后捕获撤销的 Android 定位权限?

Android - 由于设置撤销了位置权限导致应用崩溃

Android 6.0 运行时权限处理