URL ORIGEN: http://mylifewithandroid.blogspot.com/2009/06/assets.html
http://xjaphx.wordpress.com/2011/10/02/store-and-use-files-in-assets/
http://developer.android.com/reference/android/content/res/AssetManager.html
http://www.javacodegeeks.com/2012/02/android-read-file-from-assets.html
La carpeta Raw es una sub-carpeta de res, donde android guarda todos los recursos, por tanto android generará un ID automático para todo el contenido de la carpeta Raw y estos recursos podrán ser accedidos directamente desde la clase R, lo que brinda un acceso rápido a otras clases y métodos en Android
La carpeta Assets es un direcctorio adicional (un "apéndice") que se incluye en el paquete *.apk de la aplicación por tanto la clase R no genera ID's para estos archivos, por tanto es menos compatible con las clases de android y su acceso puede ser mas dificultoso para las clases y métodos. También hay un limite de 1mb para los archivos que pueden ser colocados en esta carpeta. Sin embargo el directorio es mucho más flexible y permite almacenar tipos de archivos que no pueden ser incluidos en la carpeta res y por tanto no se pueden incluir en su carpeta hija raw, lo que permite que ciertas operaciones sean más fáciles, la carpeta es ideal para por ejemplo copiar un archivo de base de datos en la memoria del sistema. No hay manera (fácil) para crear en Android una referencia XML a los archivos dentro de la carpeta assets.
El codigo a usar para implementar el asset seria el sisguiente:
01 | package pete.android.study; |
03 | import java.io.IOException; |
04 | import java.io.InputStream; |
06 | import android.app.Activity; |
07 | import android.graphics.drawable.Drawable; |
08 | import android.os.Bundle; |
09 | import android.widget.ImageView; |
10 | import android.widget.TextView; |
12 | public class Main extends Activity { |
18 | public void onCreate(Bundle savedInstanceState) { |
19 | super .onCreate(savedInstanceState); |
20 | setContentView(R.layout.main); |
22 | mImage = (ImageView)findViewById(R.id.image); |
23 | mText = (TextView)findViewById(R.id.text); |
27 | public void loadDataFromAsset() { |
31 | InputStream is = getAssets().open( "text.txt" ); |
33 | int size = is.available(); |
35 | byte [] buffer = new byte [size]; |
41 | mText.setText( new String(buffer)); |
43 | catch (IOException ex) { |
50 | InputStream ims = getAssets().open( "avatar.jpg" ); |
52 | Drawable d = Drawable.createFromStream(ims, null ); |
54 | mImage.setImageDrawable(d); |
56 | catch (IOException ex) { |
Description:
First of all, let me give you a link:
AssetManager, through this class we can easily access any files lying inside the Assets directory of android application. (or any sub-folders inside the Assets directory).
Now, we can have an object of AssetManager class by using
getAssets() method:
1 | AssetManager assetManager = getAssets(); |
And the rest of the procedure i have given and described by making comments in the example so now go through the full solutions provided below with the output snap.
Solution:
ReadFileAssetsActivity.java
01 | package com.paresh.readfileasset; |
03 | import java.io.IOException; |
04 | import java.io.InputStream; |
06 | import android.app.Activity; |
07 | import android.content.res.AssetManager; |
08 | import android.graphics.drawable.Drawable; |
09 | import android.os.Bundle; |
10 | import android.widget.ImageView; |
11 | import android.widget.TextView; |
14 | * @author Paresh N. Mayani |
17 | public class ReadFileAssetsActivity extends Activity { |
19 | /** Called when the activity is first created. */ |
22 | public void onCreate(Bundle savedInstanceState) { |
24 | super .onCreate(savedInstanceState); |
25 | setContentView(R.layout.main); |
27 | TextView txtContent = (TextView) findViewById(R.id.txtContent); |
28 | TextView txtFileName = (TextView) findViewById(R.id.txtFileName); |
29 | ImageView imgAssets = (ImageView) findViewById(R.id.imgAssets); |
31 | AssetManager assetManager = getAssets(); |
35 | String[] files = assetManager.list( "Files" ); |
37 | for ( int i= 0 ; i<files.length; i++)= "" {= "" txtfilename.append( "\n=" " file=" " :" +i+ "=" " name=" "> " +files[i]); |
39 | } catch (IOException e1) { |
47 | input = assetManager.open( "helloworld.txt" ); |
49 | int size = input.available(); |
50 | byte [] buffer = new byte [size]; |
55 | String text = new String(buffer); |
57 | txtContent.setText(text); |
58 | } catch (IOException e) { |
66 | InputStream ims = assetManager.open( "android_logo_small.jpg" ); |
69 | Drawable d = Drawable.createFromStream(ims, null ); |
72 | imgAssets.setImageDrawable(d); |
74 | catch (IOException ex) { |
main.xml
Note: Please consider scrollview as ScrollView, textview as TextView….etc. Its just problem inside the code plugin.
05 | < linearlayout android:layout_width = "fill_parent" android:layout_height = "fill_parent" android:orientation = "vertical" > |
07 | < textview android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:text = "@string/hello" android:id = "@+id/txtContent" > |
09 | < imageview android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:id = "@+id/imgAssets" > |
11 | < textview android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:id = "@+id/txtFileName" > |
12 | </ textview ></ imageview ></ textview ></ linearlayout > |
No hay comentarios:
Publicar un comentario