【android】加载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‘ :

Now, create a simple layout containing a TextView for displaying the content of ‘text.txt‘ and anImageView 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


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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值