简体   繁体   中英

READ_EXTERNAL_STORAGE permission, app crash despite I have this in manifest

I have problem with permissions. What should I do to stop my app from crashing?

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(intent, 1);
}

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

    if(requestCode == 1 && resultCode == RESULT_OK  && data != null){
        Uri selectedImage = data.getData();
        try {
            Bitmap bitmap =  MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);
            ImageView imageView = (ImageView)findViewById(R.id.imageView);
            imageView.setImageBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

AndroidManifest.xml

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

I just added this in my OnCreate method

ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 225);

And it seems to work, is there any better solution?

If putting in your onCreate() works I would check whether you put it in the manifest in the correct place because that has caused me issues in the past.

Make sure <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> is after the first manifest closing tag and before the first <Application ...

For example:

 <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="net.mypackagename">

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

    <application...

try this :

  //check permission is granted or no    
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) 
     {
      requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, RESULT_LOAD_IMG);
     } 
    else
     {
    //your code
     }

If your target build version is API 23 or higher, just check that the permission is granted or not. Go to Settings -> Apps and then tap on your app. In Permissions option enable the Storage permission. But it is not the permanent solution.

Well i do something like u want. I give u simply code how to do this :)

Here i checking permission(i call this method in fragment, make this in activity)

@TargetApi(23)
public void checkStoragePermission() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        if (settingsDrawerFragment != null) {
            settingsDrawerFragment.onPermissionGranted();
        }
        return;
    }
    if (this.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager
            .PERMISSION_GRANTED) {
        requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                REQUEST_CODE_EXTERNAL_STORAGE);
        return;
    }
    if (settingsDrawerFragment != null) {
        settingsDrawerFragment.onPermissionGranted();
    }
}

This check permission with auto message Also i make onRequestPermissions

 @Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[]
        grantResults) {
    switch (requestCode) {
        case REQUEST_CODE_AUDIO_RECORD:
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                startBrowseActivity();
            } else {
                Utils.showToast(this, getString(R.string.audio_permission_denied));
            }
            break;
        case REQUEST_CODE_EXTERNAL_STORAGE:
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                if (settingsDrawerFragment != null) {
                    settingsDrawerFragment.onPermissionGranted();
                }
            } else {
                if (settingsDrawerFragment != null) {
                    closeSettingsDrawer();
                }
                Utils.showToast(this, getString(R.string.storage_permission_denied));
            }
            break;
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            break;
    }
}

Remember to make methods in activity. On fragment check it.

Finally i make something like this to check permissions in fragment

 public void setHomeStreamSource(int position) {
    if (position == STORAGE_CLOUD_CACHE) {
        preferenceHelper.putInt(getString(R.string.preferences_usb_prefs), getString(R.string
                .preferences_video_source), MainActivity.SOURCE_CLOUD);
        closeDrawer();
        Utils.showToast(activity, getString(R.string.source_selected));
    } else if (position == STORAGE_USB) {
        ((MainActivity) activity).checkStoragePermission();
    }
}

Hope this sample code help u well

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM