Permission denied at createNewFile() in /sdcard - android

i have a piece of code, which creates a file on the /sdcard on the ExternalStorage ("internal" 8GB Memory of a GalaxyTab2 7.0).
directory = new File(Environment.getExternalStorageDirectory(),
"/logs");
directory.mkdirs();
log = new File(directory.getPath() + "/" + this.filename);
boolean created = log.createNewFile();
At the last line i get following error:
... java.io.IOException: open failed: EACCES (Permission denied)
... at java.io.File.createNewFile(File.java:948)
I have
set the permission for writing external storage in the right place in manifest
checked, that the memory is not mounted at pc
checked, that the /logs/ folder hat the correct permissions at file explorer
read every single stackoverflow thread about the topic - nothing worked
Has anyone a hint, which can cause this behavior?
best regards

add below permission in manifest file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

I used a (self-written) library, which manages all the memory access. This library project has the WRITE_EXTERNAL_STORAGE permission. The App, which references this library must also have this permission, even if it is not directly accessing the storage.

Related

saving file on sdcard with permission error

After countless searching I've managed to find path to my sdcard not the android emulated storage. But when I try to make .txt folder there it ends up with error
/storage/37F0-1515/DCIM/100MEDIA/test.txt: open failed: EACCES (Permission denied)
I don't know why because I have permissions for
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
and also I've enabled the permissions with
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, request2);
}
Here is the code that I'm using
File sdCard = new File("/storage/37F0-1515/DCIM/100MEDIA");
File dir = new File(sdCard.getAbsolutePath());
if (!dir.exists()) {
dir.mkdirs();
}
final File file = new File(dir, "test" + ".txt");
try {
BufferedWriter bufferedWriter = null;
bufferedWriter = new BufferedWriter(new FileWriter(file));
bufferedWriter.write("test");
bufferedWriter.close();
}
catch(Exception e){Log.v("myApp", e.toString());}
I don't know why android won't let me write to sdcard. Do I need some other permissions ?
I don't know why because I have permissions for
Those permissions are for external storage, not removable storage.
I don't know why android won't let me write to sdcard
You do not have arbitrary filesystem access to removable storage on Android 4.4+.
Try using these methods-
Check if the uses-permission statements are in the right place, they should be below <manifest> and above <application> tags. The correct format -
<manifest>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
...
<application>
...
<activity>
...
</activity>
</application>
</manifest>
Starting from Android Kitkat, there is a new storage policy -
The WRITE_EXTERNAL_STORAGE permission must only grant write access to the primary external storage on a device. Apps must not be allowed to write to secondary external storage devices, except in their package-specific directories as allowed by synthesized permissions. Restricting writes in this way ensures the system can clean up files when applications are uninstalled.
This can however be exploited without rooting(although I have not tried this one), as mentioned here
EDIT -
The above mentioned exploit won't work for Android 5+. There is no standard solution to this problem. Some exploits may be available but they will be device-specific/not reliable/root-access dependent.
However, tools like the Storage Access Framework can be used.

How to have the necessary permission to Write File in Removal External Storage in Android without resorting to Root

I'm using Samsung J7 Unrooted for Testing,
My AndroidManifest.xml contains the necessary permissions to write to external storage
<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_PHONE_STATE" />
I don't encounter any error when I used the code:
File myFile = new File("sdcard/Billing/mysdfile.txt");
It save the file on the Device Storage.
But when I specify the removable SDCard using the code:
File myFile = new File("/storage/extSdCard/mysdfile.txt");
I encounter the error :
open failed: EACCES (Permission denied)
I've tried different ways to point to the removable SDCard but encountered the same "open failed: EACCES (Permission denied)" error.
I've checked if I have the necessary permissions to write to external storages and it always say "Granted"
public void verifyStoragePermissions() {
if (ContextCompat.checkSelfPermission(InitActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getBaseContext(), "Denied", Toast.LENGTH_SHORT).show();
if (ActivityCompat.shouldShowRequestPermissionRationale(InitActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
} else {
}
}else{
Toast.makeText(getBaseContext(), "Granted",Toast.LENGTH_SHORT).show();
}
}
I've tried almost everything suggested listed in the google search but to no avail. There are a Few who advises to "Root" the phone but that is unfortunately not an option for me...
Hope someone can help me gain the needed permission rights to save files in my removable external storage without resorting to "Rooting" the phone.

'open failed: EACCES (Permission denied)' on Android

On Samsung T230 (Android 4.4.2) It works in both of the following
File firstFile = new File("mnt/extSdCard/Android/data/com.example.storagetest", "test.txt");
RandomAccessFile fileLittle = new RandomAccessFile(firstFile, "rw");
File scndFile = new File("mnt/extSdCard/Android/data/com.example.storagetest", "test2.txt");
scndFile.createNewFile();
I've tried a lot of these devices are working in this way. T230 is an example
On Hometech Tablet (Android 5.1.1) does not work.
File firstFile = new File("mnt/external_sd/Android/data/com.example.storagetest", "test.txt");
RandomAccessFile fileLittle = new RandomAccessFile(firstFile, "rw");
File scndFile = new File("mnt/external_sd/Android/data/com.example.storagetest", "test2.txt");
scndFile.createNewFile();
I get an error as follows:
open failed: EACCES (Permission denied)
Current AndroidManifest.xml
<application>
...
</application>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I tried many different ways... requestPermissions() and android:maxSdkVersion does not change anything
Too much search but I could not reach a conclusion.
How can i create a file on SD Card.
Important: I can read files directly, but cant change or create new file.
How can i create a file on SD Card.
You don't. Devices that ship with Android 4.4+ do not allow arbitrary access to removable storage.
You can indirectly work with removable storage via the Storage Access Framework. There, you do not worry about exactly where the user is putting the data (external storage, removable storage, Google Drive, Dropbox, etc.).

Android exception 'open failed: EACCES (Permission denied)' - not due to the SD card

I am having a problem copying files (.so binaries) from my assets directory to the app installation "/data/data/name.ofmy.app/lib" directory.
When I try to I get the following error:
07-09 11:00:48.902: W/System.err(25122): java.io.FileNotFoundException: /data/data/name.ofmy.app/lib/libOlsrdPudWireFormat.so: open failed: EACCES (Permission denied)
This happens when the following code is being ececuted:
if ((so = new File(dir, fileName)).exists()) {
/*
* Check to see if this timestamp pre-dates the current package
*/
if ((pkg = new File(ctx.getPackageResourcePath())).exists()) {
if (pkg.lastModified() < so.lastModified()) {
Log.v(TAG, "extractFile: File up to date");
return;
}
}
Log.v(TAG, "extractFile: library present but out of date");
so.delete();
}
Log.v(TAG, "extractFile: copying library");
InputStream in = ctx.getAssets().open(name);
FileOutputStream out = new FileOutputStream(so);
It breaks right at InputStream in = ctx.getAssets().open(name); when there is not an already existing file with that name...
I have been reading similar questions here on Stack Overflow, but unfortunately that did not help. Most are copying to the SD card, but I am not.
Also the strange part is that a previous directory (/bin instead of /lib), which is also supplied within my assets, does get copied without any problem in the same /dat/data... directory!
I tried adding the <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> permission to no avail.
I even tried adding Runtime.getRuntime().exec("su"); just before I try to copy anything, also to no avail.
It's a rooted phone BTW!
I also double checked if the file is really there in the assets, and it is!
Just for the sake of trying I changed the directory name from lib to libs (because as mentioned in my question, I also copied another directory named /bin without any issues) and guess what: now it works!
It seems you cannot use the /lib directoryname whilst copying. I have no idea why, though.
Definitely you forgot to put in the manifest:
<manifest
.
.
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</manifest>

Create folder on /mnt/externel1

I want to create folder on /mnt/externel1 (which is my external micro-sd card path) but when I create folder problematically [ file.mkdirs() ] it is returning false.
And when I am trying to download a file on that path by creating an outputStream it throwing an exception "Permission denied"
Note: android application not allowing to write on external micro-sd card.
your advise will helpful for me.
Please add below permission in manifest file.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
you have to mentioned permission in manifest file when you want to write into sdcard.
If you're targeting Honeycomb, you can't write to the external SD card.
Edit
Just noticed the permission stuff - you should make sure that you've got the appropriate permissions. See Chirag Raval's answer.

Categories

Resources