zoukankan      html  css  js  c++  java
  • 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();
            }
    
        }
    
    }
    

      

  • 相关阅读:
    Mysql语句练习
    Mysql-------查询各科成绩前三名的记录
    Mysql--查询"01"课程比"02"课程成绩高的学生的信息及课程分数
    模态框拖拽案例分析--元素偏移量 offset 系列
    CSS中z-index的属性与使用
    《将博客搬至CSDN》
    CSS中Position几种属性的总结
    考研数学一
    ubuntu16.04安装mysql报错解决
    LoRaWAN 规范1.0 (章节10~13)
  • 原文地址:https://www.cnblogs.com/zhengqun/p/4299151.html
Copyright © 2011-2022 走看看