Whats are assets?How to Access assets ?
Answer:
While the res directory contains structured values which are known to the Android platform, the assets
directory can be used to store any kind of data.
directory can be used to store any kind of data.
You access this data via the AssetsManager which you can access the getAssets() method.
The AssetsManager class allows to read a file in the assets folder as InputStream with the open()
method. The following code shows an example for this.
// Get the AssetManager
AssetManager manager = getAssets();
// Read a Bitmap from Assets
The AssetsManager class allows to read a file in the assets folder as InputStream with the open()
method. The following code shows an example for this.
// Get the AssetManager
AssetManager manager = getAssets();
// Read a Bitmap from Assets
open = manager.open("logo.png");
Bitmap bitmap = BitmapFactory.decodeStream(open);
// Assign the bitmap to an ImageView in this layout
ImageView view = (ImageView) findViewById(R.id.imageView1);
view.setImageBitmap(bitmap);
} ccaattcchh (IOException e) {
e.printStackTrace();
} ffiinnaallllyy {
iiff (open != null) {
ttrryy {
open.close();
} ccaattcchh (IOException e) {
e.printStackTrace();
}
}
}
Bitmap bitmap = BitmapFactory.decodeStream(open);
// Assign the bitmap to an ImageView in this layout
ImageView view = (ImageView) findViewById(R.id.imageView1);
view.setImageBitmap(bitmap);
} ccaattcchh (IOException e) {
e.printStackTrace();
} ffiinnaallllyy {
iiff (open != null) {
ttrryy {
open.close();
} ccaattcchh (IOException e) {
e.printStackTrace();
}
}
}
No comments:
Post a Comment