File Not Found Exception When Reading External Storage
Earlier the code was working totally fine, and even now it works fine for pre Android 6 devices, but in my Nexus 5, 6.0.1, I am unable to access data from external storage. It show
Solution 1:
Since your code is working fine in Pre-Marshmallow devices, looks like you've not added runtime permissions in your app.
Starting from Android M
, you need to request for permissions at runtime, as mentioned in the docs :
Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app.
Read more about Requesting permissions at runtime in Android here
Solution 2:
Make this in you onCreate()
method of main activity:
if (currentapiVersion > android.os.Build.VERSION_CODES.LOLLIPOP){
// Do something for lollipop and above versionscheckPermission();
if (!checkPermission()) {
requestPermission();
}
}
And this code out side onCreate()
:
privatebooleancheckPermission(){
int result = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION);
if (result == PackageManager.PERMISSION_GRANTED){
returntrue;
} else {
returnfalse;
}
}
privatevoidrequestPermission(){
ActivityCompat.requestPermissions(activity,newString[]{
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.CALL_PHONE},PERMISSION_REQUEST_CODE
);
}
This code will check whether the version is above Lollipop OS. If so, then it will ask for permission to user while loading app.
Hope it will help you. Tested. Working properly.
Post a Comment for "File Not Found Exception When Reading External Storage"