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‘ :

Now, create a simple layout containing aTextViewfor displaying the content of ‘text.txt‘ and anImageViewfor displaying the image ‘avatar.jpg’, which are put in Asset.

The implementation quite easy usingAssetManageras 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 intoBitmapby usingBitmapFactory.decodeStream(), instead of using Drawable.

Have fun w/ Android Coding


原文链接:https://xjaphx.wordpress.com/2011/10/02/store-and-use-files-in-assets/

更多相关文章

  1. Android(安卓)performing OAuth2 Authorization using WebView
  2. Android(安卓)API Level 与SDK版本对照表
  3. 使用Retrofit Https请求
  4. Android(安卓)TextView实现超链接
  5. Android(安卓)Lesson One: Getting Started
  6. Android练习之Linkify文字链接
  7. [置顶] 基于android2.3.5系统:Android动态库链接
  8. 【Android】Fragment
  9. Android(安卓)Studio 报Error:(29, 17) Failed to resolve: juni

随机推荐

  1. Android(安卓)Actionbar学习笔记(三)-----
  2. 从Android访问PC端的port (reverse port
  3. Android(安卓)FrameWork 系统源码调试
  4. Android应用程序的调试方法
  5. Android热插拔事件处理流程
  6. Android调节屏幕亮度工具类BrightnessUti
  7. Android Http请求方法汇总
  8. fill_parent和wrap_content 区别和使用效
  9. 今天在网上看到了Google的GPhone的消息,学
  10. Android 设置ProgressBar 的颜色