Android: make a folder on external memory private or protected - android

I want to make a folder on the external memory (could be on SD card) private or protected so that only my application/process can have access to that folder.
Can I do this? if so please let me know?
my requirement:
I can use android's internal memory to store my app's files/data, but that may decrease the phone's internal memory (that can cause problems like not having enough space for other apps to install, etc)

Data on external storage cannot be private. It is world-readable and can be changed by the user if they enable USB mass storage.
Quoted from developer.android.com:
Every Android-compatible device supports a shared "external storage" that you can use to save files. This can be a removable storage media (such as an SD card) or an internal (non-removable) storage. Files saved to the external storage are world-readable and can be modified by the user when they enable USB mass storage to transfer files on a computer.

you cannot.You can zip the folder and store in as pass-worded zip file.

Since API 9 you have for this purpose the
File.setWritable(boolean, boolean)
and also for the read/exec operation. take a look to the link

quote from android docs:
public abstract File getExternalFilesDir (String type)
Since: API Level 8 Returns the absolute path to the directory on the
external filesystem (that is somewhere on
Environment.getExternalStorageDirectory()) where the application can
place persistent files it owns.
These files are private to the
applications, and not typically visible to the user as media. This is
like getFilesDir() in that these files will be deleted when the
application is uninstalled[...]
Source: http://developer.android.com/reference/android/content/Context.html#getExternalFilesDir(java.lang.String)
So basically you will get a private (sorta) folder in your SD card.
Use this in combination with Blackbelt's answer for better effects.

Related

Preference of storage in android

I have created one app which creates one file on external memory, but when I install it on different devices the files are created in internal sdcard in some device and not created in external(Physical) sd card.
My question is that. How do we decide between the internal or external sdcard.
Which has more preference to store file by default in android?
I use the
Environment.getExternalStorageDirectory()+ "PolicyTaskfile"+"/filename.txt";
It gives external or internal sdcard path depending on the device.
The short answer is that you do not get to pick. It's up to the OEM to decide whether external storage is truly SD card or just an internal storage device (for example: eMMC). With KitKat there is a notion of primary and secondary external storage, but no easy API to determine which you are using or which is really removable media.

Get external storage parent folder on android

I've an application on the Play Store, and a user sent me a bug report saying that on his device the external sdcard is not listed in the external storage device list, though is mounted and accessible at /mnt/external_sd
I have a dialog listing folders and start directory is Environment.getExternalStorageDirectory().
After some clicks to get to the parent folder, the /storage folder is reached and getParent() returns null.
Which starting folder should I set to have access to the full storage structure?
EDIT:
As a workaround I am adding in my app preference the ability to choose if the storage root directory should be Environment.getExternalStorageDirectory() or hardcoded /mnt.
Comment/answer if there are best alternatives.
Get external storage parent folder on android
By external storage, I am assuming that you meant micro SD card storage.
external sdcard is not listed in the external storage device list,
though is mounted and accessible at /mnt/external_sd
You cannot access SD storage by using Environment.getExternalStorageDirectory() method.
Before API level 19, there was no official way to store in SD card. But, many could do it using unofficial APIs.
Officially, one method was introduced in Context class in API level 19 (Android version 4.4 - Kitkat).
File[] getExternalFilesDirs (String type)
It returns absolute paths to application-specific directories on all
shared/external storage devices where the application can place
persistent files it owns. These files are internal to the application,
and not typically visible to the user as media.
That means, it will return paths to both Micro SD card and Internal memory. Generally, second returned path would be storage path of micro SD card.
The Internal and External Storage terminology according to Google/official Android docs is quite different from what we think.

Saving files in flash memory

how can I get the path for the folder where my app can save large JPG files?. getExternalStorageDirectory() works fine only when SD Card is present, but what happens when SD is removed or the harware don't have SD Card slot.
thanks
You can use the getFilesDir() method of a Context. From a context you can also use methods to get the cache directory, external cache directory, and the external files directory. An Activity is also a context, so you can use these methods from inside one.
The getFilesDir() method gives you the folder where your files will be accessible only from your application and will be always available. However, you should use the cache directory instead, when possible. This way you will avoid making the system run out of space.
EDIT:
My answer: Almost always a device will either have an SD card or built-in external storage. When it's built-in, it's still called external storage. To check whether the external storage is removable (SD card) or built-in you can use isExternalStorageRemovable() in Environment.
Basically, you shouldn't place large files on the internal memory. There is no public folder in the internal memory. If a device doesn't have external storage, it's simply not capable of doing certain things. Simple as that. So one option you have when there is no external storage is to inform the user about it and ask them to insert a card. You don't have to handle this case, let the user handle it.
The answer you asked for: Try using getDir(String name, int mode) and/or openFileOutput(String name, int mode) of a Context object, and for mode use MODE_WORLD_READABLE or MODE_WORLD_WRITEABLE. Also check Using the Internal Storage.
You are facing intended limitations of the platform that are there for the good of everyone.
You could either require external directory to exist, or you can store to the internal directory. If you choose to permit both, I suggest you store a flag in internal space to indicate that you've stored something externally, so that if external storage is not present you can take appropriate action.
As you are Saving Large JPG files, its is better to save it in external storage because Phone has very small internal memory and its all effect the performance of phone.

Way to save application cache in internal sdcard in android?

In my application , downloading lot of files from server. I want to cache them in sdcard.
for that am using fallowing api..
context.getExternalCacheDir();
But problem is that, not able to save them in internal sdcard(i.e; non removable external storage).They are saving in to "/mnt/sdcard/external_sd/Android/data/".
Please gimme a way to save may files in to non removable android cache.
Regards,
Srinivas
From intellisense for Environment.getExternalStorageDirectory:
"Gets the Android external storage directory. This directory may not currently be accessible if it has been mounted by the user on their computer, has been removed from the device, or some other problem has happened. You can determine its current state with getExternalStorageState().
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.
In devices with multiple "external" storage directories (such as both secure app storage and mountable shared storage), this directory represents the "primary" external storage that the user will interact with.
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. Other shared files should be placed in one of the directories returned by getExternalStoragePublicDirectory(String)."

What are the differences among Internal storage, external storage, sd card and removable storage?

From the official documentation:
Using the External Storage
Every Android-compatible device supports a shared "external storage"
that you can use to save files. This can be a removable storage
media (such as an SD card) or an internal (non-removable)
storage ...
I'm really confused about this subject. From what I understand, using external storage doesn't necessarily mean to use a removable card, am I right? However, when talking about external storage, it's always referred as "sd card".
I'm developing an app that downloads .mp3 files from the internet. I want to save those files in the phone memory (don't want to use any removable device) but for what I have learned, those files have to be saved in external memory.However, I would like to offer the possibility of importing a file from a removable device. Where and how should I save those files?
Thanks
Difference between Internal Storage, External Storage (aka primary external storage) and Secondary External Storage?
Internal Storage: is storage that is not accessible by the user, except via installed apps (or by rooting their device). Example: data/data/app_packageName
External Storage has two types:
Primary External Storage: In built shared storage which is "accessible by the user by plugging in a USB cable and mounting it as a drive on a host computer". Example: When we say Nexus 5 32 GB.
Secondary External Storage: Removable storage. Example: SD Card.
When building an app that uses the internal storage, the Android OS creates a unique folder, which will only be accessible from the app, so no other app, or even the user, can see what's in the folder.
The external storage is more like a public storage, so for now, it's the sdcard, but could become any other type of storage (remote hard drive, or anything else).
The internal storage should only be used for application data, (preferences files and settings, sound or image media for the app to work).
If you intent to download many mp3s, i'd reccomend saving them to external storage, as the external storage is often bigger. Besides, storing data on the internal storage may prevent the user to install other applications.
The Internal and External Storage terminology according to Google/official Android docs is quite different from what we think.
According to official Android docs:-
Internal Storage: By default, files saved to the internal storage are private to your application and other applications cannot access them. When the user uninstalls your application, these files are removed/deleted. Your app user also can't access them using file manager; even after enabling "show hidden files" option in file manager. To access files in Internal Storage, you have to root your Android phone. So, this is NOT what we think as internal memory of the phone - Nexus 5's 32 GB internal memory.
External Storage:
This can be a removable storage media (such as an SD card) or an
internal (non-removable) storage
That means, both storage types like Nexus 6P's 64 GB internal memory and removable microSD card which we insert in phone's card slot are considered as External Storage.
Removable Storage means just microSD card storage, not the internal memory.
To store your app files in SD card, you may use File[] getExternalFilesDirs (String type) method in Context class. Generally, second returned path would be the storage path for microSD card (if any).
Note: I have edited - made my answer more useful after #Tunaki's comment.
From the Developer docs
All Android devices have two file storage areas: "internal" and "external" storage. These names come from the early days of Android, when most devices offered built-in non-volatile memory (internal storage), plus a removable storage medium such as a micro SD card (external storage). Some devices divide the permanent storage space into "internal" and "external" partitions, so even without a removable storage medium, there are always two storage spaces and the API behavior is the same whether the external storage is removable or not.
I think in the operating system, it defines external storage as anything not related to the actual OS filestructure. If you recall, when you write to 'internal storage', Android will make a folder privately for your application. So basically, if this is a hidden folder of some kind, it would mean that external storage could qualify as anything not being automatically hidden or managed directly by the OS. So this would mean that it would be up to the phone manufacturer about the definition of internal storage, as they could have 1 main piece of internal flash memory with two partitions on it. One partition meant to hold the os and the other meant to let you store everything on the phone.
Basically what I'm saying is: That's more a hardware related thing, and that the concept of 'external storage' could extend even to extra internal storage (flash memory) that the manufacturer added in. You could even consider storage options defined by the user as external storage as well.
Here's an updated answer for the latest Android (currently Android 13).
Internal storage used to mean the phone's internal memory and external storage used to mean, among other things, any inserted SD cards. Nowadays, this is not really the case because phones don't have SD cards any more. Phones without SD card slots still have "external storage" from the point of view of an app (eg Environment.getExternalStorageDirectory() still returns a valid location), but it's emulated - meaning it's actually a slice of internal storage.
This means one of the big differences between external and internal storage - which was that external was slower but bigger and internal fast and small - is no longer true.
Since Android 11, external storage has been scoped. This means apps get a folder of their own which is readable to them and unreadable to any other app (though see below). They can access this folder without permissions. Again, this brings external storage in line with internal. (Note scoped storage actually appeared in Android 10 but has only been enforced since Android 11).
But there are remaining differences between internal and external.
One big difference is that apps can still get a permission to read/write across all of external storage (MANAGE_EXTERNAL_STORAGE). This is hard to come by - users need to manually enable it by going into Settings, and apps which implement it are severely limited on the Play Store.
But it does mean that data written to external storage is less secure than that written to internal. Other apps may be able to eavesdrop on external.
The above is the TL;DR but there's a bit more to it. Here's a good article: https://medium.com/#tdcolvin/demystifying-internal-vs-external-storage-in-modern-android-c9c31cb8eeec

Categories

Resources