How to Write Files to Assets Folder or Raw Folder in Android

How to write files to assets folder or raw folder in android?

It cannot be done. It is impossible.

Android - Write file to assets folder


Since I cannot write any files to assets or any raw dir, where it will be written?

openFileOutput() writes to internal storage.

What will the location of that file be? Where will it be located in the Android file system ?

That can vary by device and user. It will be to the same directory that is returned via a call to getFilesDir().

How to write a file in Raw folder in android

Any data present in the "res" folder is not writable. You can only read data from it. You can never write data to res folder on the fly. If you are looking for a way to store username and password credentials, you can make use of Shared Prefrence

Here is an example on how to use it.

http://marakana.com/forums/android/examples/63.html

Edit 1

Storing data in Shared Preference is persistent unless user clears the data using "clear data" button in your settings page.Navigate to settings->manage apps->your app. Here you will be seeing uninstall button and clear data button. And one more thing in android is you will never be able to save a persistent data. You can't use any data storage methods like preference, sqlite or file system to store a data for permanent. If user wants to wipe the data he clicks on "Clear Data" button and your data are gone. So you have make your coding in such a way to handle this.

Since this is not possible, you could try to use your app's resources which is write protected and not possible to write to it. So it depends on user or you might have to use your server to store the data over there.

Should an extra file be placed in raw or assets folder?

assets/ is flexible in terms of structure. You can have your own directory tree in there, for organizing lots of files.

res/raw/ is flexible in terms of resource sets. You can have different versions of that resource for different configurations (e.g., res/raw/ as the default, res/raw-zh/ for use on devices configured for Chinese language use).

If you do not need any of those features, assets/ and res/raw/ are about equal in terms of capability. I tend to default to assets/.

Write in a txt file located in /raw folder


I have a txt file in the /raw folder and I want to write into it

That is not possible at runtime. Resources and assets are read-only at runtime.

But gives me error in openRawResource()... How do i solve this?

openRawResource() is for reading in the resource, and it gives you an InputStream. You are welcome to write your data to an ordinary file, such as on internal storage.

How to write to a json file which is stored in asset folder?

You can't write to asset folder. because it's a read-only folder. Instead, you need to save the file to your app folder. Whenever you want to use the config, check if the file is existed in your app folder. if it's exist, use it, if not, use the default one.

For example, when you get the config.json, save the file:

String filename = "config.json";
String fileContents = "Your config content..";
FileOutputStream outputStream;

try {
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(fileContents.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}

Then, whenever you want to use, read it:

File file = new File(context.getFilesDir(), "config.json");

String config = "";
if(file.exists()) {
// use the config.
} else {
// use the config from asset.
}

Read more at Save Files on Device Storage for saving the file.

When should I use the assets as opposed to raw resources in Android?

The main differences between the raw folder and the assets folder.

  • Since raw is a subfolder of Resources (res), Android will
    automatically generate an ID for any file located inside it. This
    ID is then stored in the R class that will act as a reference to
    a file, meaning it can be easily accessed from other Android classes
    and methods and even in Android XML files. Using the automatically
    generated ID is the fastest way to have access to a file in Android.

  • The assets folder is an “appendix” directory. The R class does
    not generate IDs
    for the files placed there, which is less compatible
    with some Android classes and methods. File access in the assets folder is slower since you will need to get a handle to it
    based on a String
    . However some operations are more easily done by
    placing files in this folder, like copying a database file to the
    system’s memory. There’s no (easy) way to create an Android XML
    reference to files inside the Assets folder.

Trying to get the path for a file in the assets or raw directory


I want to get the path so I can pass it to a function and load it. However, I can’t seem to get the path

That is because there is no path. Those are not files. They are entries in the APK file.

I get FileNotFoundException Exception and the and the string contains: android.content.res.AssetManager$AssetInputStream@5363f738

That is because you called toString() on an InputStream returned from the AssetManager.

Source snippet for loading the keystore

load() takes an InputStream. You do not have to use a FileInputStream. You are welcome to pass the InputStream that you get from the AssetManager to load():

KeyStore keyStoreFile = KeyStore.getInstance(KeyStore.getDefaultType());
keyStoreFile.load(resources.getAssets().open("snapzkeystore.bks"), password);

Just a quick questions, which is the best folder to put a keystore in, raw or assets?

Either should be fine. You can use openRawResource() on Resources, IIRC, to get an InputStream on a raw resource.

Save a (.txt) file to the Assets Folder in Android?

You can't. The assets folder is read-only at runtime.

Pick a different location to save your data, see Data Storage for more information.

Writing to android assets directory at runtime?

The asset directory is read only, you can not change it dynamically.

You can read & white files in the flowing directories:

Context.getFilesDir()  

No permission required

Typical position: /data/data/app.package.name/files/

Context.getExternalFilesDir(null)  

Requires WRITE_EXTERNAL_STORAGE when api level <= 18

Typical position: /sdcard/Android/data/app.package.name/files/

Environment.getExternalStorageDirectory()  

Requires WRITE_EXTERNAL_STORAGE

Typical position: /sdcard/



Related Topics



Leave a reply



Submit