Store and use files in Assets
There are times when you probably want to your application distribution with raw resources, instead of pre-defined resources, the ‘res‘ folder, you gonna have to make use of ‘Asset‘.
‘Assets’ folder will be distributed along with the APK, which contains all the raw files you need for application, such as: text files (.txt), non-Android XML files (.xml), Audio files (.wav, .mp3, .mid)…; those cannot be put into ‘res‘ folder as usual.
Thing needed to be looked up here: AssetManager from Android Developers’ References
This class does the job that we need.
First, create a project as usual, then put files into ‘asset‘ like below:
Now, create a simple layout containing a TextView for displaying the content of ‘text.txt‘ and an ImageView for displaying the image ‘avatar.jpg’, which are put in Asset.
The implementation quite easy using AssetManager as mentioned above.
package pete.android.study;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class Main extends Activity {
ImageView mImage;
TextView mText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mImage = (ImageView)findViewById(R.id.image);
mText = (TextView)findViewById(R.id.text);
loadDataFromAsset();
}
public void loadDataFromAsset() {
// load text
try {
// get input stream for text
InputStream is = getAssets().open("text.txt");
// check size
int size = is.available();
// create buffer for IO
byte[] buffer = new byte[size];
// get data to buffer
is.read(buffer);
// close stream
is.close();
// set result to TextView
mText.setText(new String(buffer));
}
catch (IOException ex) {
return;
}
// load image
try {
// get input stream
InputStream ims = getAssets().open("avatar.jpg");
// load image as Drawable
Drawable d = Drawable.createFromStream(ims, null);
// set image to ImageView
mImage.setImageDrawable(d);
}
catch(IOException ex) {
return;
}
}
}
That’s a quick sample code giving this result.
The distributed APK file contains the folder ‘assets‘, you might wanna check by opening it.
Quite easy, isn’t it?
@p/s: you can load image from Asset into Bitmap by using BitmapFactory.decodeStream(), instead of using Drawable.
Have fun w/ Android Coding
Cheers,
Pete Houston


Hi pete, thank you for the tutorial. I encountered an “out of memory error” when i tried to implement this. The image file I’m trying to display is 23.1kb with dimensions 318×333. I encounter the same error when i try to use BitmapFactory.decode stream. Is there any way around this without having to scale down the image file
Can you give me the .xml file to display the textview and imageview am a not a Pro
Can you give me the .xml file to display the textview and imageview am a not a Pro
Can you help me?
Loading files kml from the Raw folders ! How?
Thank you,
What directory is talking about.
Because the menu Window> ShowView> Other … > File Explorer is a directory with the path data / data / application packages would be this external directory?
I can pull a file from that directory component webview? example: webview.loadUrl (“file :/ / / data / data / pages_web / examples / / index.html”);
I can create a directory within a file and save it as?
He understood my question, I just need to know to create directories and write files htmls updated inside.
how do I write to a file in the Asset and can say the name of the directory and file name? HELP ME
Sorry, you can’t do that, directory is where you put all necessary stuffs for your apps and packing the final APK.
Just make copy of a file in your and write/read to your internal storage in run-time…it’s totally fine.