Confusion in External Storage in android - android

1)Question
I have used installLocation in manifest
android:installLocation="preferExternal"
Correct me if i am wrong. android:installLocation="preferExternal" make your app install on external sdcard . If this is true then i should not be able to use myapplication when sdcard is removed . But however i can use myapplication even when i removed sdcard from my device
2)Question
what is the difference between sdcard0 and extsdcard on my device ?
even when i use in my manifest
android:installLocation="preferExternal"
all app files are stored on sdcard0 and it is accessible even when i have sdcard in my device . i have used below method to store files on sdcard
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
So in which condition my files will be stored on my sdcard ?
and how can i store files on extsdcard?

1) As stated in the docs:
If you declare "preferExternal", you request that your application be
installed on the external storage, but the system does not guarantee
that your application will be installed on the external storage.
Unmounting the external storage means that all running apps that were installed there will be immediately killed. If you are still able to use your app, I see 2 possible reasons: the system decided to install your app on the internal storage OR your device also features an emulated "external" storage and your app was installed there.
2)
what is the difference between sdcard0 and extsdcard on my device
It's common that sdcard0 refers to the device internal storage while extsdcard refers to the external storage that might be physical or emulated. However there's no reliable naming convention, it's up to the vendor.
Also note that "external" an Android basically means "a storage that the user can access".
A few further steps that might help:
use ADB Shell to examine your device's storage structure, with SD card attached and detached
verify your app's install location from the "Apps" settings menu
Log absolute paths of directories provided by different methods of the Environment class
Note: generally, you should never rely on assumptions of your app's install location since that might get you in trouble on some devices

Related

Force app to install on internal storage

I want my app to be installable only on internal storage.
I have tried two options:
Not specifying any storage preference in the manifest (the Docs say that this forces the app to install only to internal storage)
Explicitly specifying android:installLocation="internalOnly" in the manifest
In both cases, when I try to install it on a tablet that's running Android 4.4.2, Kernel 3.4.67, and having an SD card, it prompts me to select whether the app should be installed on Tablet Storage or the SD card.
The tablet has ample free internal storage (9 GB). The default write disk is also set to Internal Storage.
Is there any way to bypass this prompt and install to internal storage after the user approves the permissions needed by the app?
According to the docs, the flag you are using should not allow install on the SDCARD or any external storage. This appears to be a bug.
Post at the bug location to help give it attention.

Where is my sdcard location on 4.4.2?

On Android 4.4.2 Environment.getExternalStorageDirectory().getPath() returns /storage/emulated/0 but this path does not exist on my Nexus5 Android 4.4.2. Environment.getExternalStorageDirectory().getPath() worked up until Android 4.4.2.
How can I get the /sdcard path on Android 4.4.2?
This path does not exist on my Nexus5 Android 4.4.2.
Yes, it does, for your process at runtime.
For example, this sample project downloads a file to Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS). If you log the location at runtime, when running it on a Nexus 5, it is reported as /storage/emulated/0/Download. And the download succeeds.
If you are looking for /storage/emulated/0 via DDMS or adb shell, you will not find it. For those tools, default external storage is /mnt/shell/emulated/0. Hence, the downloaded file from the above sample appears in the /mnt/shell/emulated/0/Download directory.
AFAIK, the difference is tied to providing separate external storage to secondary accounts.
The Storage Options documentation says to use Environment.getExternalStorageDirectory() (as you are already correctly using). This function is available on all versions of Android.
Are you seeing it return a path that isn't actually available on a 4.2 device?
Please note (from Environment.getExternalStorageDirectory()):
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).
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.
Starting in KITKAT, if your application only needs to store internal data, consider using getExternalFilesDir(String) or getExternalCacheDir(), which require no permissions to read or write.
Sometimes /storage/emulated/0 can be written to, but reads fail... so tests for "writability" are not sufficient. This is such an annoying problem, I have come up with an equally annoying but effective solution.
Hardcode "/mnt/sdcard" Yea, I said it.
Looks like someone else said it first ... storing android application data on SD Card
More joy... http://forums.bignerdranch.com/viewtopic.php?f=414&t=7407

Why there are two folders sdcard and sdcard2 under /mnt on my android device

When using Eclipse file explorer to navigate my android directories, I saw mnt/sdcard and mnt/sdcard2, see below image:
When callingEnvironment.getExternalStorageDirectory() it returns mnt/sdcard, so I think the mnt/sdcad is the external storage , and mnt/sdcard2 is my actual SD card, is that true? And how can I use code to access files under mnt/sdcard2 ?
P.S.
It seems that I can access the external sd card directly:
File extStorageDir = Environment.getExternalStorageDirectory();
String parent = extStorageDir.getParent();
File extSdCardDir = new File(parent+"/sdcard2");
File file = new File(extSdCardDir, "DemoFile.jpg");
But I wonder the extra sd card will change name in other cases.
You are correct, getExternalStorage will return your built-in external storage. Unfortunately, as of Jelly Bean applications are no longer able to utilize the SD card if the device also has built-in storage as well as an SD card. You can try working around it through shell commands or hardcoding paths, but without root there is no reliable way to access it anymore.
This was just recently added the Android CTS, which all OEMs must comply with in order to use the Play store.
Compatibility Program Overview | Android Developers
Section 9.5 (pg. 34) of Android 4.3 Compatibility Definition
Device implementations that include multiple external storage paths
MUST NOT allow Android applications to write to the secondary external
storage.
Storage Options | Android Developers
It's possible that a device using a partition of the internal storage
for the external storage may also offer an SD card slot. In this case,
the SD card is not part of the external storage and your app cannot
access it (the extra storage is intended only for user-provided media
that the system scans).
Android 4.2 APIs | Android Developers
Saving data in a multi-user environment
Whenever your app saves user preferences, creates a database, or
writes a file to the user’s internal or external storage space, that
data is accessible only while running as that user.
To be certain that your app behaves properly in a multi-user
environment, do not refer to your internal app directory or external
storage location using hard-coded paths and instead always use the
appropriate APIs:
For access to internal storage, use getFilesDir(), getCacheDir(), or openFileOutput().
For access to external storage, use getExternalFilesDir() or getExternalStoragePublicDirectory().
No matter which of these APIs you use to save data for a given user,
the data will not be accessible while running as a different user.
From your app’s point of view, each user is running on a completely
separate device.

How to prevent Android app installation if SDCard absent?

My code logic needs an SD card installed in the device. I have added a check for this case in the application's splash screen, but would like to inform users before they download/install this app. Is there a way to achieve this ?
Thanks !
There is no way to do this before the app installs, as the only way to limit such things is by using the <uses-feature> tag. However, that tag has no options for storage requirements. The best warning you can give is to prominently include it in your app description.
On the other hand, every device I've ever heard of an encountered has some form of external storage, be it a SD Card or inbuilt memory mounted as external storage. What you're doing by using the Splash Screen to check for the external storage is the best way to do this, as there is no other option.
There's no way to do that. Your app have to be installed to be able to check user's environment. You could try to to enforce SD card installation of your app, so if there's none Google Play might (not tested) simply not allow app installation at all, but it will not solve your problem as user will still do not know why. Solution is to clearly state in product description that SD card is mandatory. But note, that requiring SD card is risky as many devices does not have any while still offer external storage. My suggestion - just add note about storage requirements and let system deal with it.
I think it is NOT POSSIBLE . You are checking the sdcard on splash screen and prevent user for next process is the right solution or Use android:installLocation for install android application on sdcard.
Beginning with API Level 8, you can allow your application to be
installed on the external storage (for example, the device's SD card).
This is an optional feature you can declare for your application with
the android:installLocation manifest attribute.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:installLocation="preferExternal"
... >
If you declare "preferExternal", you request that your application be
installed on the external storage, but the system does not guarantee
that your application will be installed on the external storage. If
the external storage is full, the system will install it on the
internal storage. The user can also move your application between the
two locations.
When your application is installed on the external storage:
There is no effect on the application performance so long as the
external storage is mounted on the device.
The .apk file is saved on the external storage, but all private user
data, databases, optimized .dex files, and extracted native code are
saved on the internal device memory.
The unique container in which your application is stored is encrypted
with a randomly generated key that can be decrypted only by the
device that originally installed it. Thus, an application installed
on an SD card works for only one device.
The user can move your application to the internal storage through
the system settings.
Look Here for more details .

external-internal storage

In android, whats the difference between external storage that is nonremovable (internal) and the internal storage? I am not sure where to save my data. I just need to save a game stats that a person can pull anytime they want
Thank you
In many newer devices there will no longer be a physical distinction, with "internal" and "external" storage sharing the same flash chips without necessarily even having fixed allocation, so the difference really remains one of intended access paradigm.
Files on the external storage (real or simulated) are unavoidably shared with everything, and modifiable by anything with that manifest permission. Traditionally they are easily visible, though with the recent MTP-access devices the system may need to be told about them before a connected PC will see them.
In contrast, files on the internal storage are private to an application, excepting anything running as root or system, or if the application has decided to make them generally visible or changeable. Even when accessible, data on the internal storage can be more difficult to access outside the owning app - it's not supported by the consumer-targeted USB Mass Storage or MTP mechanisms, and even for other apps and development tools it is tricky to locate unless you know where to look, as while you may be able to examine files folders which applications have chosen to share, you cannot browse through the parent (typically /data or /data/app) folders. It can be a bit frustrating even for the developer of an app to access the files it creates on private storage during debugging (though while the apk is debuggable it is possible with the run-as tool and adb, or you can build an export capability in to the app, or run on the emulator where adb has root).
Likely your decisions process should be something like this: Is it meant to be private to the owning application? If so, put it on the internal storage, unless it's too big and targeted at older devices, in which case you may need to encrypt and/or sign it for protection before placing on the external storage. Otherwise, if it's meant to be shared, needs to be handed to arbitrary other components (email app, etc), or is big, put it on the external storage.
In android, whats the difference between external storage that is nonremovable (internal) and the internal storage?
External storage never meant removable. It always meant "accessible by the user by plugging in a USB cable and mounting it as a drive on a host computer". Early Android devices happened to have removable external storage, but that was never the definition.
Internal storage is storage that is not accessible by the user, except via installed apps (or by rooting their device).
Further to Chris' answer if you are concerned about external storage (SD card), not being avilable you can simply check this every time your app loads up and then pull in the relvent information accordingly.
You can use something like this:
if(isSDPresent)
{
// SD Card is present
// Your code goes here, files will be located # /mnt/sdcard/Android/data/packagename
}
else
{
// SD Card is not presenet
// files located here /data/data/packagename
}
If you are testing on an emulator, you can load up your DDMS and find all the files stored in the revlent places.
Hope this helps.

Categories

Resources