0

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" />
Yury Fedorov
  • 14,508
  • 6
  • 50
  • 66
wegtis
  • 303
  • 2
  • 4
  • 12

5 Answers5

0

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?

wegtis
  • 303
  • 2
  • 4
  • 12
  • Yes, you should handle what happens when the user does not grant permission. See docs. Also, if not necessary, you should ask for the permission only when the you need the permission, not in the onCreate, for it will be then more understandable for the user what and why you are asking for. – lionscribe Aug 22 '16 at 02:29
0

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...
ShadowGod
  • 7,891
  • 3
  • 28
  • 31
  • But if you are targeting API 23 or greater you have to specifically ask for that permission in your code (i.e. at runtime) .. It's not hard, Google has the docs for that on their site somewhere. – ShadowGod Aug 22 '16 at 00:38
0

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
     }
Khalil M
  • 1,788
  • 2
  • 22
  • 36
0

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.

Sourav Bagchi
  • 656
  • 7
  • 13
0

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

Genehme
  • 69
  • 9