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.).
Related
Currently
I'm storing my file(images/videos) like this:
File directoryToStore;
directoryToStore = getBaseContext().getExternalFilesDir("MyImages");
This will return this path:
/storage/emulated/0/Android/data/pacageName/files/MyImages/
Now, I want to store the files in root directory /storage/emulated/0/MyImages/. I have tried this by doing:
File directoryToStore;
directoryToStore = new File(Environment.getExternalStorageDirectory(), "MyImages");
This works perfectly fine when running on pre-Marshmallow devices, but In Marshmallow the files are not found.
My Question
How should/can I store files in the root directory so that the file will be found in all API's?
Firstly, make sure you have the permissions bellow in your Android Manifest file, because you are trying to save files into the device's external storage.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Secondly, check if storage permissions are granted to your application (when you execute on Marshmallow or higher OS version devices).
Finally, make sure if there is directory named "MyImages" and check it's chmod. If directory does not exist, create it (mkdir() will create the directory in your example) and then try to save your files again.
The directory was not able to write in android 4.4.2
File pdfDir=newFile(Environment.getExternalStoragePublicDirectory
(Environment.DIRECTORY_PICTURES),"PdfFolder");
Also, i used manifest WRITE_EXTERNAL_STORAGE
If your app has requested the WRITE_EXTERNAL_STORAGE permission, then it is always permitted to write to Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) even in KitKat, as getExternalStoragePublicDirectory() points to external storage, which is not removable storage ("SDCard") on the vast majority of devices.
File file = new File(getExternalFilesDir(null), "PdfFolder")
this worked for me.
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.
I'm using BeagleBuddy mp3 tag editor to make changes to mp3 files on the internal / external storage... Works fine on some phones... but on certain phones, I get this error... (Samsung S4, Note 3) etc...
open failed: EACCES (Permission denied)
I have in my manifest...
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Still no luck... Any suggestions?
I've read a lot about how Android is protecting the external card from edits on some phones... is there a way to get around this?
I've used several different methods to get the external drive... Some work on some phone, some don't work...
This is what I am currently using:
public static File getRemovableStorage()
{
String value = System.getenv("SECONDARY_STORAGE");
if (!TextUtils.isEmpty(value))
{
String[] paths = value.split(":");
for (String path : paths)
{
File file = new File(path);
if (file.isDirectory())
{
return file;
}
}
}
return null; // Most likely, a removable micro sdcard doesn't exist
}
It finds all of the files on the external drive great... I just get that error when I try to update an mp3 tag on the external drive.
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.