Android M运行时权限问题

时间:2016-03-29 11:13:21

标签: android bitmap permissions android-6.0-marshmallow video-thumbnails

我已经对这个问题进行了很多搜索,但我找不到任何解决方案,为什么会这样呢?

目前我遇到了运行时权限的奇怪问题,我试图询问运行时对相机的权限并且它工作正常但我的问题是当用户允许我的代码从库中挑选视频时使用ThumbnailUtils创建缩略图,但是当我的应用程序第一次安装时它将返回null我的意思是当用户第一次允许权限时它将返回null后所有的情况thumbnailutil工作正常,以及下面的android M它的工作正常时间也是。

我目前正在做的是

要求获得适当的许可,并在检查后允许或拒绝我根据我的要求处理来自图库的选择视频

-----------------相机权限-----------------

 public void requestPermissionForCamera() {
    if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.CAMERA)) {
        Toast.makeText(activity, "Camera permission needed. Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show();
    } else {
        ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.CAMERA}, CAMERA_PERMISSION_REQUEST_CODE);
    }
}

-----------------外部存储权限--------------------

  public void requestPermissionForExternalStorage() {
    if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
        Toast.makeText(activity, "External Storage permission needed. Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show();
    } else {
        ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE);
    }
}

以下是我的onRequestPermissionsResult& onActivityResult

更新

 @Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case REQUEST_WRITE_STORAGE: {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {


             //   my code for open video or image gallery
            } else {
                //SnackBar  "The app was not allowed to write to your storage. Hence, it cannot function properly. Please consider granting it this permission"
            }
        }
    }

}


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

    if (resultCode == FragmentActivity.RESULT_OK) {
        if (requestCode == SELECT_VIDEO) {

            Uri videoUri = data.getData();
            selectedVideoPath = getRealPathFromURI(videoUri);

            if (selectedVideoPath == null) {
                Log.e("vediotag", "selected video path = null" + selectedVideoPath);

            } else {
                Log.e("vediotag", "selected video path----" + selectedVideoPath);

            //create video thumbnail
                Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(selectedVideoPath, MediaStore.Video.Thumbnails.MINI_KIND);
                insertData(mimageTitle, bitmap, selectedVideoPath);

            }
        }
    }

}


public String getRealPathFromURI(final Uri contentURI) {
    Cursor cursor = getActivity().getContentResolver().query(contentURI, null, null, null, null);
    if (cursor == null) { // Source is Dropbox or other similar local file path
        return contentURI.getPath();
    } else {
        cursor.moveToFirst();
        int idx = cursor.getColumnIndex(MediaStore.MediaColumns.DATA);
        if (idx == -1) {
            return contentURI.getPath();
        }
        String rvalue = cursor.getString(idx);
        cursor.close();
        return rvalue;
    }
}

我目前面临的问题

我的代码上面的代码在活动重新创建一次后工作正常,但是当我安装应用程序并允许权限时,它首次失败。

  

注意:    我不想以编程方式重新创建活动,因此不要建议我这样做。

非常感谢任何帮助

问候。

0 个答案:

没有答案