I am developing an app that needs direct access to the root folder of the sdcard, however it seems in Android 4.2, the standard /sdcard directory now points to an "emulated" sdcard specific to the user running the app. This is not good, as my app requires access to a file that is stored on the top level of the sdcard. Does anyone know how to directly access the sdcard in Android 4.2?
Perhaps I am misunderstanding your question, however, with my Nexus Galaxy running Android 4.2.1 I can access my sd card using Environment.getExternalStorageDirectory(). The returned directory is /storage/emulated/0, but the content is that of /sdcard.
Can you use the storage directory as a File type? (java.io.File)
If so, you can get the external storage (Typically SD card, but will be main storage on phones with no SD card) by using code such as this in your method:
File path = Environment.getExternalStorageDirectory();
Additionally, access of storage requires READ_EXTERNAL_STORAGE permission in your Android Manifest - with WRITE_EXTERNAL_STORAGE being needed if any data is modified. Here are these permissions as they would appear in the manifest:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Sources:
http://developer.android.com/reference/android/os/Environment.html
http://developer.android.com/reference/android/Manifest.permission.html
Related
I have give read and write permissions in manifest file and with code. I am able to create files in phone storage with no problems and its absolute path is /storage/emulated/0
But When I try to write file in my removable sdcard with file.createNewFile() to thw path /mnt/media_rw/33A9-10F2, it throws an error
java.io.IOException: Permission denied
It is possible to download a file from that location using Download manager. Please help me to solve this issue...
You do not have read/write access to arbitrary locations on removable storage on Android 4.4+.
DownloadManager is powered by a system app, and system apps have greater ability to work with the filesystem than do ordinary apps.
I am able to create file in phone storage with no problems.
You do not have access to most of external storage on Android 10 (by default) and Android R+ (for all apps).
I am developing an application where I need to download pdf files from firebase storage and store it inside the app(the one I am developing) and these files shouldn't be accessible to user outside my app. I had developed using Firebase code for downloading but it is downloading in the download folder of mobile even after changing the destination directory.
Nowadays there are two kinds of storage in an android device: Internal and external.
According to android docs:
Internal storage:
It's always available.
Files saved here are accessible by only your app.
When the user uninstalls your app, the system removes all your app's files from internal storage.
External storage:
It's world-readable, so files saved here may be read outside of your control.
It's not always available, because the user can mount the external storage as USB storage and in some cases remove it from the device.
When the user uninstalls your app, the system removes your app's files from here only if you save them in the directory from getExternalFilesDir().
External storage is the best place for files that don't require access restrictions and for files that you want to share with other apps or allow the user to access with a computer.
Internal storage is best when you want to be sure that neither the user nor other apps can access your files.
To get path of internal storage of your app:
getFilesDir()
This will give you a File representing the directory on the file system that's uniquely associated with your app.
Hope it helps!
Source: Save files on device storage
EDIT:
If you want the file downloaded from firebase storage to be only visible to your app, you should move the file from downloads to internal storage and then delete the file from downloads
I tries to create a new folder in External storage SD Card. Using this code:
val folderMain = "NewFolder"
val f = File(Environment.getExternalStorageDirectory(), folderMain)
if (!f.exists()) {
f.mkdirs()
}
and this permission:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
But after executing this code It creates a new folder in Internal storage. Here is the screenshot of Internal Storage:
While External Storage has no new folder in it. Here is the screenshot of External Storage:
What should I do? I want to create a new folder in External storage By using Kotlin or java codes.
From https://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory()
Note: don't be confused by the word "external" here. This directory
can better be thought as media/shared storage. It is a filesystem that
can hold a relatively large amount of data and that is shared across
all applications (does not enforce permissions). Traditionally this is
an SD card, but it may also be implemented as built-in storage in a
device that is distinct from the protected internal storage and can be
mounted as a filesystem on a computer.
I am trying to write to External secondary storage (sd card), but it looks like I do not have the permission.
According to Android:
"Secondary external storage must not be writable by apps, except in package-specific directories as allowed by synthesized permissions."
So this wont work:
try {
String sdCard = System.getenv("SECONDARY_STORAGE");
FileWriter writer = new FileWriter(new File(sdCard, "test.txt"));
writer.write("a test");
writer.flush();
writer.close();
} catch (IOException e) {
Log.d(e.toString());
//java.io.FileNotFoundException: /storage/extSdCard/test.txt: open failed: EACCES (Permission denied)
}
I have the permission in AndroidManifest.xml <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
and Environment.getExternalStorageDirectory() wont work since I am trying to write to removable sd card not the phone memory.
So, if apps supposed to be prohibited to write to secondary storage, how can apps like File Manager write to sd cards?
So this wont work:
Among other reasons, I am not aware of a requirement for devices to have a SECONDARY_STORAGE environment variable.
I have the permission in AndroidManifest.xml
That is for external storage, not removable storage.
how can apps like File Manager write to sd cards?
They are relying on users helping them via the Storage Access Framework.
My Motorola Phone has 12GB internal storage and Removable Sd card feature.
In DDMS file explorer my internal phone storage(12GB) is mounted as sdcard and my removable sdcard is mounted as sdcard-ext.
Using "Environment.getExternalStorageDirectory().getAbsolutePath()" method i can able to get the directory "/mnt/sdcard".
Is there is any method through which i can find the absolute path of my removable sdcard?... ie.,which will return me "/mnt/sdcard-ext"
The Android framework currently supports only one mass storage device, so it's hard to manage multiple (you could try to parse /proc/mounts to get all mounted devices).
But you can use Motorolas API that they built because of that fact.
Check out the Motorola "External" Storage API.
Try this code its return all storage folder name.
Check My post here:
https://stackoverflow.com/a/38930531/1733810
Don't forget to add permission.
Getting access to external storage
In order to read or write files on the external storage, your app must acquire the READ_EXTERNAL_STORAGE or WRITE_EXTERNAL_STORAGE system permissions. For example:
<manifest ...>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
...
</manifest>