• android访问asset目录下的资源


    android提供了AssetManager来访问asset目录下的资源,

    在activity中通过getAssets()获取AssetManager

    常用的api如下:

    1、列举路径下的资源String[] list(String path)

    2、InputStream open(asset目录下的资源路径)

    下面是放问asset目录下的图片的代码

    package com.example.qunzheng.customerview;
    
    import android.app.Activity;
    import android.content.res.AssetManager;
    import android.graphics.BitmapFactory;
    import android.graphics.drawable.BitmapDrawable;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.ImageView;
    
    import java.io.IOException;
    import java.io.InputStream;
    
    
    public class VisitAssetsResourceActivity extends Activity {
    
        private ImageView imageView;
        private String[] images = null;
        private AssetManager assetManager;
    
        private int curImageIndex = 0;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_visit_assets_resource);
    
            imageView = (ImageView) findViewById(R.id.image);
            assetManager = getAssets();
            try {
                images = assetManager.list("");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public void viewImage(View view) {
            if (curImageIndex >= images.length) {
                curImageIndex = 0;
            }
            /**
             * 如果图片资源还没有释放,则释放该资源,防止内存溢出
             */
            BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
            if (drawable != null && drawable.getBitmap() != null && !drawable.getBitmap().isRecycled()) {
                drawable.getBitmap().recycle();
            }
    
            try {
                InputStream assertFile = assetManager.open(images[curImageIndex++]);
                imageView.setImageBitmap(BitmapFactory.decodeStream(assertFile));
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }
    
    }
    

      

  • 相关阅读:
    日记功能发布
    功能小改进
    CuteEditor 6.0一个很奇怪的Bug
    关于博客园.NET俱乐部的公告
    新功能发布
    建立了一个博客园创业者QQ群
    思想决定生活 《卡耐基成功之道全书》读书笔记
    建立了一个英语学习小组
    实际上我没有选择的余地
    公告
  • 原文地址:https://www.cnblogs.com/zhengqun/p/4299151.html
Copyright © 2020-2023  润新知