I am new to android . I have little confusion on internal storage topic in android. Please correct me if i am worng
InternalStorage :- Allows us to read and write file that are associated with each application's internal memory so the data are private to your app.
This file can only be accessed by the application and cannot be accessed by other application or users
Is it true that InternalStorage store data in device internal memory ?
If answer is yes then how can i store images ,videos etc that user can see and access it on device or by pc when they are connected through usb?
if my app downloads an image and device does not have sdcard and i have used internalstorage method in this condition how user can see dowloaded image using usb or from device ?
It will be good if explanation is given with example .Thanks in advance
You must save your files on external storage. Notice that external storage does not automatically mean removable storage. Normally the devices built-in storage is divided into internal and external storage. However, external storage also includes removable storage like SD cards.
So, if you want to save files that the user should be able to access for example with his PC you should save to the external storage.
The respective user guides on that topic are in particular:
https://developer.android.com/training/basics/data-storage/files.html#InternalVsExternalStorage
https://developer.android.com/guide/topics/data/data-storage.html
For example, if you want to save a picture that is accessible by the user in the expected directory you would use
File picture = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "filename.png");
Since API level 17, MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE are deprecated in internal storage method.so, you cannot write a file that can be accessed by user using internal storage method.
try External Storage to write file so that you can access.for more
https://developer.android.com/guide/topics/data/data-storage.html
Related
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.
Where does downloaded files stored in android when there is no memory card and how to access it from my application?
They are stored in device's internal storage, if you haven't chosen to store it in the sd-card. Internal storage make use of linux file system permissions, so files will private to your application and other applications cannot access them.
If you are talking about media files, you can make them accessible everywhere by adding them in their respective content providers. To open a file use openFileOutput() which return a file stream.
You should read the article on Storage Options which also provides a code example for accessing the external storage.
Can anyone help me ? Is it possible to create shared preference files in sdcard rather than on private path of an android application.
You cannot modify where shared preferences are stored.Its a private storage. if you want to use sd card use
Environment.getExternalStorageDirectory(). and get the path of directories stored in sdcard.
it is possible by reading&writing the xml file into the external storage ,but it's not the same as sharedPreferences. you will have to implement your own way , or use the android code of this class.
however , there are some disadvantage over using the internal storage:
anything you store on the sdcard is visible to every application and the end user can read it by just opening it.
only in the internal storage you get some sort of protection against reading the file , so that only rooted devices can read the files.
external storage can also be unmounted , so the data can sometimes be unreachable. you need to handle possible errors that can occur because of this.
uninstalling the app while the sd card is mounted means that the data will stay on the sdcard .
I want to create a file and I don't want it to be deleted when the application data is cleared. Is there a way for this in android, something like permanent storage ?
Yes, you can save to external storage (typically the SD card). See the section in the docs entitled Saving files that should be shared:
If you want to save files that are not specific to your application
and that should not be deleted when your application is uninstalled,
save them to one of the public directories on the external storage.
You can use SQLite for permanent storage. It will do the job perfectly. Avoid using GetExternalFilesDir() if you do not want the data to be deleted when the application removed.
This tutorial from Google will tell you about all options for storage
Data Storage in 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.