I'm trying to read a file from a folder on my Galaxy S4. When I place the file in the root directory, I can access it without problems:
File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "MyFile");
But I want to put my file in a subdirectory. When I make a folder 'A', place my file inside of it and try to access it with:
File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separatorChar + "A", "MyFile");
I can't read it. Do I need some kind of permission and what's the logic behind that, when I can access the root?
You can read here:
http://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory()
that access to this folder is not recomended:
Applications should not directly use this top-level directory, in order to avoid polluting the user's root namespace. Any files that are private to the application should be placed in a directory returned by Context.getExternalFilesDir, which the system will take care of deleting if the application is uninstalled.
Also, permission is required:
Writing to this path requires the WRITE_EXTERNAL_STORAGE permission, and starting in read access requires the READ_EXTERNAL_STORAGE permission, which is automatically granted if you hold the write permission.
[edit]
Also, since you are using s4 - which is probably 4.4+ device, you should know that since KitKat Google has disallowed write access on removable media, you can only read it, or write it to your application folder:
http://commonsware.com/blog/2014/04/09/storage-situation-removable-storage.html
As a result, apps can read files on removable media using the various undocumented and unsupported tricks for finding removable media. However, apps cannot write to or otherwise modify such removable storage. Note that device manufacturers themselves may have ways of dealing with this, but ordinary app developers do not.
Related
As we know from android 11 Write external storage permission will not work, we cant access the other directories of root path of external storage, But my question is what about the File Manager App, if my app is File Manager than how do I Manage all file operation as like before. for example, new file or folder creation, here we can not get root path access of external storage, so how to manage File Manager operation, I read in the developer site and somewhere their is option to handle that particular apps, and Google also give some way to manage that app, but how I cant find some solution, If anyone know please help me out from this.
As we know from android 11 Write external storage permission will not work, we cant access the other directories of root path of external storage,
Requesting write external storage makes no sense as it is implicitly granted to your app already.
You can access all paths from getExternalStorageDirectory() and getExternalStoragePublicDirectory().
And create subdirs and files in the getExternalStoragePublicDirectory()'s
As per the Documentation
Use scoped storage unless your app needs access to a file that's stored outside of an app-specific directory and outside of a directory that the MediaStore APIs can access. If you store app-specific files on external storage, you can make it easier to adopt scoped storage by placing these files in an app-specific directory on external storage. That way, your app maintains access to these files when scoped storage is enabled.
In my case I am creating a text file using below:
File root = new File(this.getExternalFilesDir(null), "sample");
if (!root.exists()) {
root.mkdirs();
}
File file = new File(root, "sample.txt");
Above is creating a file at below location (while having NO SD card available):
/storage/emulated/0/Android/data/<package-name>/files/sample.txt
And I am performing writing operations on it.
Below are the queries:
While referring to the mentioned documents it is still not cleared if Scoped storage enforcement is applicable for the above-mentioned scenario? Do I need to implement the Scoped Storage or It will work without any changes and there is no impact of Android 11 behavior change.
As per my understanding There ks no Impact of Scoped storage enforcement due to Android 11. As I don't have the use case where the app needs access to a file that's stored outside of an app-specific directory. Is this understanding correct?
In case if I want to read a file that is placed under the Downloads folder, I need to implement the Scoped Storage because my app wants to access the file which is stored outside of an app-specific directory. Is this understanding correct?
You are definitely allowed to make and access files in /storage/emulated/0/Android/data/ using the File API. As far as I can tell there's no change to those locations. I have done it.
For access to files in Download yes, scoped storage is required from what I've read. I haven't tested this but the docs are pretty clear that it's the law in Android 11. A bad law, but the law nonetheless.
I'm trying to create an android application that writes to a text file that can later be accessed once a button is pushed.
The past week i've tried a bunch of methods that people suggest to write to the internal storage, and sometimes it appears to work (using an outputwriter, and also a File class?), but i'm never able to locate the file on the Android device I test-run it on.
I'm rather new to development for Android, so all this is confusing to me.
Thanks
If by "internal storage" you mean what the Android SDK refers to as internal storage, this is not possible. Files that you create there are only accessible to your app, not by file managers on or off the device.
If by "internal storage", you mean what the Android SDK refers to as external storage, you need to:
Get a File pointing to a directory on external storage, such as calling getExternalFilesDir() on some Context, like your Activity
Create that directory if it does not exist
Create a File object pointing to the file you want to create, off of that directory
Use standard Java file I/O to write to the location identified by that File
Use MediaScannerConnection and its scanFile() method to tell Android "hey, I just put a file on external storage, please index it so it shows up in file managers"
Also:
Ideally, you do the disk I/O on a background thread, so you do not freeze the UI while that work is going on.
Depending on your minSdkVersion and where you choose to write the file, you may need the WRITE_EXTERNAL_STORAGE permission
Depending on your targetSdkVersion, you may need to ask for WRITE_EXTERNAL_STORAGE at runtime
You CAN access internal storage sometimes, but this depends on your phone.
You always can get data from internal memory for rooted phones (need root).
Files are in the folder /Android/data/
Some vendors allows you to run root shell on non-rooted phone through adb
(I saw this behaviour on Explay tabet) just run adb shell su to
test. Then you can copy your file from internal storage to public
with shell commands.
adb pull may also work in this case. (Again
vendor dependent)
There are many questions about this topic, but I cannot find any answers for my corrective example.
I'm using Samsung galaxy S5 run android 4.4, which is limited for storage
The official document said:
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.
My application need to write files to Sdcard (Absolute path is /storage/extSdCard), so I write my app data to my app directory: /storage/extSdCard/Android/com.myapp.example/files but got permission denied exception. So I suspect the above statement:
except in their package-specific directories as allowed by synthesized
permissions
I think I cannot write to root directory /storage/extSdCard but still able to write my app data to my app package directory. Did I misunderstand something here?
p/s: I still able to write my app data to built-in storage: /storage/emulated/0/Android/data/com.myapp.example/files. I don't want to use getExternalFileDirs() because it always return built-in, not my sdcard directory.
If getExternalFilesDir(null) is returning somewhere different to /storage/extSdCard/Android/com.myapp.example/files, then I would think that is why it is giving you access permission errors. The only place on the SD card you can write to without permissions is the directory returned by getExternalFilesDir()
Since you say the directory returned by getExternalFilesDir(null) is not acceptable, I would suggest adding the WRITE_EXTERNAL_STORAGE permission to your manifest.
Your app specific directory should be /storage/extSdCard/Android/data/com.myapp.example/files and not /storage/extSdCard/Android/com.myapp.example/files
mySound.load(new URLRequest("file://mnt/sdcard/AnyFolder/YourSound.mp3"));`<br/>
I want to detect the name of the sdcard at runtime, so i can store my app-data on external storage.
The above codeline is an example of storing a mp3 to user's required location.
Maybe you are asking for:
File f = new File(Environment.getExternalStorageDirectory() +
"/" + "yourfilename");
I also recomend you to avoid writing to the root of the SD card. It is a good practice to write your aplication data into the appropriate folder:
File f = new File(Environment.getExternalStorageDirectory() +
"/Android/data/com.example.testapp/" + "yourfilename");
Obviously, you have to change the "com.example.testapp" with your app package (the package you entered when you created the project in Eclipse/Android Studio).
getExternalStorageDirectory()
in
http://developer.android.com/reference/android/os/Environment.html
use Environment.getExternalStorageState() it will give path to internal SD mount point like "/mnt/sdcard"
First, on Android 4.4+, you do not have write access to removable media (e.g., "external SD"), except for any locations on that media that might be returned by
getExternalFilesDirs() and getExternalCacheDirs().
See Dave Smith's excellent analysis of this, particularly if you want the low-level details.
Second, lest anyone quibble on whether or not removable media access is otherwise part of the Android SDK, here is Dianne Hackborn's assessment:
...keep in mind: until Android 4.4, the official Android platform has not supported SD cards at all except for two special cases: the old school storage layout where external storage is an SD card (which is still supported by the platform today), and a small feature added to Android 3.0 where it would scan additional SD cards and add them to the media provider and give apps read-only access to their files (which is also still supported in the platform today).
Android 4.4 is the first release of the platform that has actually
allowed applications to use SD cards for storage. Any access to them
prior to that was through private, unsupported APIs. We now have a
quite rich API in the platform that allows applications to make use of
SD cards in a supported way, in better ways than they have been able
to before: they can make free use of their app-specific storage area
without requiring any permissions in the app, and can access any other
files on the SD card as long as they go through the file picker, again
without needing any special permissions.