AssetManager中常用的list()方法和open()

        android系统为每个新设计的程序提供了/assets目录,这个目录保存的文件可以打包在程序里。/res和/assets的不同点是,android不为/assets下的文件生成ID。如果使用/assets下的文件,需要指定文件的路径和文件名。下面这个例子,显示如何用list方法与open方法访问/assets下的内容。

        准备工作:在文件中/assets目录下放几张图片,布局很简单,一个ImageView 一个Button。单击按钮会显示下一张图片。

        布局文件如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="300dp"
         />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>


      Android文档中对list(),open()的介绍如下:

         一:list()   

public final java.lang.String[] list(java.lang.String path)
                              throws java.io.IOException
Return a String array of all the assets at the given path.
Parameters:
path - A relative path within the assets, i.e., "docs/home.html".
Returns:
String[] Array of strings, one for each asset. These file names are relative to 'path'. You can open the file by concatenating 'path' and a name in the returned string (via File) and passing that to open().
Throws:
java.io.IOException
    参数是文件路径名,返回的是String数组。

    二:open()

public final java.io.InputStream open(java.lang.String fileName)
                               throws java.io.IOException
Open an asset using ACCESS_STREAMING mode. This provides access to files that have been bundled with an application as assets -- that is, files placed in to the "assets" directory.
Parameters:
fileName - The name of the asset to open. This name can be hierarchical.
Throws:
java.io.IOException
     参数是文件名,返回的是InputStream流。默认ACCESS_STREAMING模式。
重载的有:
public final java.io.InputStream open(java.lang.String fileName,
                       int accessMode)
                               throws java.io.IOException
accessMode有:ACCESS_UNKNOWN, ACCESS_STREAMING, ACCESS_RANDOM, ACCESS_BUFFER

程序文件如下:

public class MainActivity extends Activity {
    int currentImg = 0;
    String[] images;
    AssetManager assets;
    ImageView image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         image = (ImageView) findViewById(R.id.imageView1);
        try{
             assets = getAssets();
            //获取/assets目录下所有文件
            images = assets.list("");

        }catch(IOException e)
        {
            e.printStackTrace();
        }
        
        final Button bn = (Button) findViewById(R.id.button1);
        bn.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                //找下一个图片文件
                while(!images[currentImg].endsWith(".png")&&
                        !images[currentImg].endsWith(".jpg")&&
                        !images[currentImg].endsWith(".gif"))
                {
                    currentImg++;
                    currentImg%=images.length;
                }
                InputStream assetfile = null;
                try{
                    //打开指定资源的输入流
                    assetfile = assets.open(images[currentImg++]);

                    currentImg%=images.length;
                }catch(IOException e){
                    e.printStackTrace();
                }
                BitmapDrawable bitmapDrawable = (BitmapDrawable) image.getDrawable();
                //如果图片还没回收,先强制回收该图片
                if(bitmapDrawable != null && !bitmapDrawable.getBitmap().isRecycled()){
                    bitmapDrawable.getBitmap().recycle();
                }
                //改变显示的图片
                image.setImageBitmap(BitmapFactory.decodeStream(assetfile));
            }
        });

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值