Android - READ_EXTERNAL_STORAGE权限在重新启动

时间:2016-04-04 03:18:01

标签: android thumbnails android-permissions permission-denied mediastore

制作图库应用我使用MediaStore光标查询查询特定文件夹的图像。

当我需要访问设备内存时,遵循Lolipop及以上版本的新权限API,我会检查权限是否可用,如果没有,我会要求它。授予权限后,我重新启动活动,光标成功地显示图像条目(表示授予权限)。

但是后来在代码流程中,我尝试检索这些条目的位图,并使用此方法获得权限错误:

Bitmap bm=MediaStore.Images.Thumbnails.getThumbnail( context.getContentResolver(), imageId, MediaStore.Images.Thumbnails.MICRO_KIND, null);

LogCat说:

E/ThumbnailUtils(22666): java.io.FileNotFoundException: /storage/emulated/0/Music/New Folder 2/subfolder/img.jpg: open failed: EACCES (Permission denied)

如果我强制关闭应用程序并使用System.exit(0)重新启动它,一切正常并且成功检索到位图。我不明白发生了什么......这是操作系统中的错误吗?任何帮助???

我的许可逻辑:

String permission = Manifest.permission.READ_EXTERNAL_STORAGE;
if(ContextCompat.checkSelfPermission(this,  permission) == PackageManager.PERMISSION_GRANTED;){
     getImagesFromFolder(folderPath);
}
else{
    if(ActivityCompat.shouldShowRequestPermissionRationale(athis, permission)){
        ActivityCompat.requestPermissions(this, new String[]{permission}, requestCode); 
    }
    else{
        showPermissionProblemDialog();
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch(requestCode){
    case REQUEST_READ_STORAGE_PERMISSION:
        finish();
        if(grantResults[0]==PackageManager.PERMISSION_GRANTED){
            startActivity(getIntent().addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION));
            overridePendingTransition(0, 0); 
        }
        else{
            F.toastLong(con, "ImageGallery cannot open without the read external storage permission");
        }
        break;
    }
}


public void getImagesFromFolder(String folderPath){
    String sortBy=MediaStore.Images.Media.DATE_MODIFIED+" desc";
    String selection=MediaStore.Images.Media.DATA +" like?";    
    String[] selectionArgs=new String[]{"%"+folderPath+"%"};
    String[] projection = {MediaStore.Images.Media._ID,MediaStore.Images.Media.DATA,MediaStore.Images.Media.DATE_MODIFIED,MediaStore.Images.Media.BUCKET_ID, MediaStore.Images.Media.BUCKET_DISPLAY_NAME};
    Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    Cursor cursor = getContentResolver().query( uri, projection,  selection, selectionArgs, sortBy); 
    .
    .
    .
}

1 个答案:

答案 0 :(得分:0)

在按钮上单击访问图库的位置,尝试此权限模型。

       if (ContextCompat.checkSelfPermission(Signup.this,
                    Manifest.permission.READ_EXTERNAL_STORAGE)
                    != PackageManager.PERMISSION_GRANTED) {
                Log.d("PERMISSIONS", "Manifest.permission.READ_EXTERNAL_STORAGE: Denied");
                if (ActivityCompat.shouldShowRequestPermissionRationale(Signup.this,
                        Manifest.permission.READ_EXTERNAL_STORAGE)) {
                    Log.d("PERMISSIONS", "Manifest.permission.READ_EXTERNAL_STORAGE: Requesting with explanation?");
                    Snackbar.make(v, "Allow accessing your gallery.", Snackbar.LENGTH_INDEFINITE)
                            .setAction("GRANT", new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    ActivityCompat.requestPermissions(Signup.this,
                                            new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                                            1);
                                }
                            })
                            .show();

                } else {
                    Log.d("PERMISSIONS", "Manifest.permission.READ_EXTERNAL_STORAGE: Requesting implicitly");
                    ActivityCompat.requestPermissions(Signup.this,
                            new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                            1);
                }
            } else {
                Log.d("PERMISSIONS", "Manifest.permission.READ_EXTERNAL_STORAGE: Granted");
                Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(i, RESULT_LOAD_IMAGE);
            }

请务必覆盖onPermissionRequestResult

 @Override
 public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case 1: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Log.d("PERMISSIONS", "Manifest.permission.READ_EXTERNAL_STORAGE: Granted");
                Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(i, RESULT_LOAD_IMAGE);

            } else {

                Toast.makeText(this, "Cannot access gallery. Try again.", Toast.LENGTH_SHORT).show();
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}