Android - Saving file to internal memory - android

I have file in Assets and I need to write this file to the Internal memory (not to the private part as a /data/data/mypackage/files, but to the memory I am able to see as a removable disk, when I am connected to PC). Is there any way how to achieve this? I don't mean the method for file copy, but how to access my internal memory?

Here is a discussion that answers this problem. I'm assuming that you want to copy your asset to the SD card.
How to copy files from 'assets' folder to sdcard?

I'm not entirely certain about what you are asking, it sounds like you want to write to the SD card, but that's also usually considered external memory. If this is what you mean though, here is the documentation you need to read: http://developer.android.com/guide/topics/data/data-storage.html#filesExternal

Related

How to get path of directory which is in SD Card?

I want to create directory in SD card same as in the internal storage.
My internal storage path is "sdcard/<my_directory_name>/"
I want to create the same directory in root of SD card.
I have try the following ways to find the path of directory.
sdcard1/<my_directory_name>/
Environment.getExternalStorageDirectory() +"/<my_directory_name>/"
Please suggest the way to find SD card path.
You do not have arbitrary access to removable storage on Android 4.4+. Hence, there is no useful path, from a filesystem standpoint. You are welcome to use getExternalFilesDirs() and kin -- if they return 2+ locations, the second and subsequent ones are on removable storage, and you can read and write to those locations.
Although as CommonsWare answered, you cannot read or write in Secondary External Storage, here's a way to generate it's path.
System.getenv("SECONDARY_STORAGE"); //returns /storage/extSdCard
You can also use String path = "/storage/extSdCard"; but again you cannot write files there.
Edit: As #CommonsWare commented, there is no guarantee of this method. Though when I tested it, it worked, but again, if he says there's no guarantee, you can take his word.

Where do you put downloaded filed in android

My application downloads files from the server and save them somewhere. Where should I put the folder to save the files? Not in assets right? Should I create a folder somewhere parallel to "bin"/"libs"/"res" folder?
Another question: should I put the downloaded files in internal storage or external storage?
Actually you can not save the downloaded file in either of this folders because they are not allowed to make modification at run time.
Best option goes according to your requirement, as if you file is too large and you are not much worried about it's security then you can store them on SDCARD(External storage). and if your file is small enough and much secure then you should save them on internal storage.
Check most popular application store their files on SDCARD but encrypted way...
Hope this ans your question
Store your downloaded files in a folder on an external storage directory. like
String yourFolderPath = Environment.getExternalStorageDirectory() + /com.yourapp/;
Store files in that folder.
if its just few files, it would be safer and more convenient to use the context.getCacheDir(). with it, you can be sure that it will always be there unlike sdcard which can be locked when the phone is in storage mode. it doesnt require manifest permission. You don't even have to worry about security unless your user is rooted.

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.

Reading From a file in android that is private to the Application

I am developing an android application and i need to read words from a file to create a Trie. Is there a way i can write the file to internal storage on install so that only my application can access it, or should i hard code the words. Any suggestions are appreciated
If the file isn't going to change, you should put it in your assets directory. If it is going to change, you can copy it from your assets to internal storage and it'll be private. External storage (often an SD card) is word-readable.
http://developer.android.com/guide/topics/data/data-storage.html
http://developer.android.com/reference/android/content/res/AssetManager.html

Android: make a folder on external memory private or protected

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.

Categories

Resources