Android Manager之AssetManager

AssetManager主要用于存储一些常用的资源,供项目使用。比如存放图片,Json文件,TextView自定义字体等等资源。下面讲解一下每个资源的使用方法。

 

 

1.获取使用图片

 

 

 

 

private Bitmap getImageFromAssetsFile(String fileName) {
    Bitmap image = null;
    AssetManager am = getResources().getAssets();
    try {
         InputStream is = am.open(fileName);
         image = BitmapFactory.decodeStream(is);
         is.close();
    } catch (IOException e) {
         e.printStackTrace();
    }
     return image;
}

 

ImageView imageView=findViewById(R.id.activity_event_imageview);
imageView.setImageBitmap(getImageFromAssetsFile("img_3.jpg"));

 

 

 

 

 

 

2.获取使用Json文件

 

public static String getJson(String fileName, Context context) {
    StringBuilder stringBuilder = new StringBuilder();
    try {
        //获取assets资源管理器
        AssetManager assetManager = context.getAssets();
        //通过管理器打开文件并读取
        BufferedReader bf = new BufferedReader(new InputStreamReader(assetManager.open(fileName)));
        String line;
        while ((line = bf.readLine()) != null) {
            stringBuilder.append(line);
        }
     } catch (IOException e) {
         e.printStackTrace();
     }
     return stringBuilder.toString();
}

 

String result = getJson("Two.json", this);
Log.d("TAG","result----:"+result);

 

 

 

 

 

3.获取assets目录下的文件设置TextView字体

 

AssetManager assetManager = activity.getAssets();

Typeface typeface = Typeface.createFromAsset(assetManager, "XXX路径/XXX.ttf");

textView.setTypeface(typeface);

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值