I am new to android and I am working in an android existing project.The app is crashing on android version >6.0, with below exception.Basically app is selecting photo from gallery which is working fine for the first time and on second time onwards the app is crashing giving permission denial exception.
java.lang.SecurityException: Permission Denial: reading com.google.android.apps.photos.contentprovider.MediaContentProvider uri content://com.google.android.apps.photos.contentprovider/0/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F1022/ORIGINAL/NONE/256350537 from pid=7789, uid=10145 requires the provider be exported, or grantUriPermission()
I have gone through few links and checked that android has introduce run time permissions and I have used below code to check the runtime permission.
The things I have tried so far...
- Added permission in manifest.
2.Checking the runtime permission from code.
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
Log.d("Enter", "onRequestPermissionsResult: ");
switch (requestCode){
case REQUEST_CODE_PERMISSION:{
Map<String,Integer> perms = new HashMap<>();
//Initialize the map with the permissions
perms.put(Manifest.permission.ACCESS_COARSE_LOCATION,PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.CAMERA,PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.READ_EXTERNAL_STORAGE,PackageManager.PERMISSION_GRANTED);
// perms.put(Manifest.permission.READ_USER_DICTIONARY,PackageManager.PERMISSION_GRANTED);
//Fill with actual results from user
if (grantResults.length > 0){
for (int i = 0 ; i < permissions.length ; i++){
perms.put(permissions[i],grantResults[i]);
//check for all permissions
if (perms.get(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED){
Log.d("Permission Granted", "onRequestPermissionsResult: ");
}else{
Log.d("Some", "onRequestPermissionsResult: ");
//if (perms.get(Manifest.permission.ACCESS_COARSE_LOCATION))
if (ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.CAMERA)
|| ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.ACCESS_COARSE_LOCATION)
|| ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.READ_EXTERNAL_STORAGE)){
new DialogInterface.OnClickListener(){
@Override
public void onClick (DialogInterface dialog, int which){
switch (which){
case DialogInterface.BUTTON_POSITIVE:
checkAndRequestPermission();
break;
case DialogInterface.BUTTON_NEGATIVE:
break;
}
}
};
}else{
Toast.makeText(this,"Go to Settings and enable Permissions",Toast.LENGTH_LONG).show();
}
}
}
}
}
}
}
private void showDialogOK(String message, DialogInterface.OnClickListener okListener){
new AlertDialog.Builder(this)
.setMessage(message)
.setPositiveButton("OK",okListener)
.setNegativeButton("Cancel",okListener)
.create()
.show();
}
}
And the line where it is crashing is :-
if (checkAndRequestPermission()){
InputStream fis = getContentResolver().openInputStream(Uri.parse(url)); //Crashing Line
BitmapFactory.decodeStream(fis, null, o);
fis.close();
}
Below are the permissions used in My Manifest:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_USER_DICTIONARY"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS"/>
<uses-permission android:name="com.google.android.apps.photos.permission.GOOGLE_PHOTOS"/>
<!-- <uses-permission android:name="com.google.android.apps.photos.permission.GOOGLE_PHOTOS"/>-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<!-- The following two permissions are not required to use
Google Maps Android API v2, but are recommended. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="com.google.android.apps.photos.permission.GOOGLE_PHOTOS"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-feature android:name="android.hardware.location" android:required="true" />
<uses-feature android:name="android.hardware.location.gps" android:required="false" />