Getting all the time permission denied or no such file or directory by trying to save Bitmap image. What should i do?
runtime permissions letting user to allow or deny any permission at runtime. use this lib Dexter library.also check an working exmple here
Include the library in your build.gradle
dependencies{
implementation 'com.karumi:dexter:4.2.0'
}
this example requests WRITE_EXTERNAL_STORAGE.
Dexter.withActivity(this)
.withPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
.withListener(new PermissionListener() {
@Override
public void onPermissionGranted(PermissionGrantedResponse response) {
// permission is granted, open the camera
}
@Override
public void onPermissionDenied(PermissionDeniedResponse response) {
// check for permanent denial of permission
if (response.isPermanentlyDenied()) {
// navigate user to app settings
}
}
@Override
public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {
token.continuePermissionRequest();
}
}).check();
Requesting Multiple Permissions
To request multiple permissions at the same time, you can use withPermissions()
method. Below code requests STORAGE
and LOCATION
permissions
.
Dexter.withActivity(this)
.withPermissions(
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.ACCESS_FINE_LOCATION)
.withListener(new MultiplePermissionsListener() {
@Override
public void onPermissionsChecked(MultiplePermissionsReport report) {
// check if all permissions are granted
if (report.areAllPermissionsGranted()) {
// do you work now
}
// check for permanent denial of any permission
if (report.isAnyPermissionPermanentlyDenied()) {
// permission is denied permenantly, navigate user to app settings
}
}
@Override
public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) {
token.continuePermissionRequest();
}
})
.onSameThread()
.check();
Can't save bitmap: ENOENT (No such file or directory)
You may apply the following code -
final int MyVersion = Build.VERSION.SDK_INT;
if (MyVersion > Build.VERSION_CODES.LOLLIPOP_MR1) {
if (!checkIfAlreadyhavePermission()) {
ActivityCompat.requestPermissions(YourActivity.this, new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
} else {
your_method();
}
} else {
your_method();
}
private void your_code(){
//Your entire code will go here
}
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) {
your_method();
} else {
Toast.makeText(getApplicationContext(), "Please provide access to external storage", Toast.LENGTH_LONG).show();
}
break;
}
}
}
java.io.IOException: No such file or directory (save image)
There were major changes on how files can be accessed on Android 10
See https://developer.android.com/training/data-storage
You need to use MediaStore or Storage Access Framework (SAF), details https://developer.android.com/training/data-storage/shared for files outside of your App's private directories.
As you are storing photo then MediaStore would be the way to access pictures
https://developer.android.com/training/data-storage/shared/media
Though as a quick fix is to temporarily opt out https://developer.android.com/training/data-storage/compatibility but this will only work until Android 11
Some better examples at https://proandroiddev.com/working-with-scoped-storage-8a7e7cafea3
setImageBitmap() from sdcard doesn't display
are you add permission for write external storage
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
int permissionCheck = ContextCompat.checkSelfPermission(
this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]
{
Manifest.permission.WRITE_EXTERNAL_STORAGE}, 33);
}
}
Android unable to save image file to SD card
Error:
Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such
file or directory) at libcore.io.Posix.open(Native Method)
basically your image file is not found because file path you have given is wrong.
Use Environment.getExternalStorageDirectory()
to get Path of SD Card.
Remove the .getAbsolutePath()
and it will be fine. Environment.getExternalStoreDirectory()
will give you the path to wherever the manufacture has set their external storage.
Try
String file_path = Environment.getExternalStorageDirectory().toString()+"/OpenGL"
instead of
String file_path = Environment.getExternalStorageDirectory()+ "/OpenGL";
EDIT1:
You need to add this permission to write file into Internal storage.
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
I recommend you to save image in Internal Storage rather that SD card because Different manufacturer use different SD card name so there is different SD card path for different devices.
EDIT2:
Try this code
String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/OpenGL";
File dir = new File(file_path);
if(!dir.exists()){
dir.mkdirs();
}
String format = new SimpleDateFormat("yyyyMMddHHmmss", java.util.Locale.getDefault()).format(new Date());
File file = new File(file_path, format + ".png");
instead of
String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/OpenGL";
File dir = new File(file_path);
if(!dir.exists()){
dir.mkdirs();
}
String format = new SimpleDateFormat("yyyyMMddHHmmss", java.util.Locale.getDefault()).format(new Date());
File file = new File(dir, format + ".png");
EDIT3:
give both permission in manifest file:
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
and also try to use both dir.mkdir();
and dir.mkdirs();
with above 2 permissions.
Permission denied on writing to external storage despite permission
If you're running your app on API level 23 or greater you have to request permission at runtime.
Request permission:
String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
requestPermissions(permissions, WRITE_REQUEST_CODE);
Then handle the result:
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case WRITE_REQUEST_CODE:
if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
//Granted.
}
else{
//Denied.
}
break;
}
}
For more information visit Requesting Permissions at Run Time - Android Doc
Related Topics
Multiple Table SQLite Db Adapter(S) in Android
Add a New Item to Recyclerview Programmatically
Waiting for Target Device to Come Online
Add 'Tools:Replace="Android:Value"' to <Meta-Data> Element at Androidmanifest
Change Expandable Indicator in Expandablelistview
Masked Input Using Edittext Widget in Android
Android Translate Animation - Permanently Move View to New Position Using Animationlistener
Android Webview: Download Files Like Browsers Do
Writing Text File to Sd Card Fails
Configuration on Demand Is Not Supported by the Current Version of the Android Gradle Plugin
Android Sharedpreferences in Fragment
Android Task Affinity Explanation
How to Work with Android's In-App Update API
Getting Height of Text View Before Rendering to Layout
Android Google Maps V2: How to Add Marker with Multiline Snippet
Transfer Data from One Activity to Another Activity Using Intents