Android中使用assets下的资源——图片资源


在Android 应用中使用assets目录下存放的资源文件,assets目录下存放的资源代表应用无法直接访问的原生资源,应用程序通过AssetManager以二进制流的形式来读取资源。此应用是查看/assets/目录下的图片查看器(图片格式为:.png),在assets目录下放几张PNG格式的图片

该程序的界面十分简单,只包含一个ImageView和一个按钮

代码如下:

布局文件如下:bitmaptest.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:orientation="vertical"  
  5.   android:gravity="center_vertical"  
  6.   android:layout_width="wrap_content"  
  7.   android:layout_height="wrap_content">  
  8.   <Button  
  9.   android:id="@+id/btnBitmap"  
  10.   android:layout_width="wrap_content"  
  11.   android:layout_height="wrap_content"  
  12.   android:text="TestBitmap"  
  13.   />  
  14.   <ImageView  
  15.   android:id="@+id/imageBitmap"  
  16.   android:layout_width="wrap_content"  
  17.   android:layout_height="wrap_content"    
  18.   />  
  19.   
  20. </LinearLayout>  

java源代码:

  1. package com.infy.configuration;  
  2. import java.io.IOException;  
  3. import java.io.InputStream;  
  4.   
  5. import android.app.Activity;  
  6. import android.content.res.AssetManager;  
  7. import android.graphics.BitmapFactory;  
  8. import android.graphics.drawable.BitmapDrawable;  
  9. import android.os.Bundle;  
  10. import android.view.View;  
  11. import android.view.View.OnClickListener;  
  12. import android.widget.Button;  
  13. import android.widget.ImageView;  
  14.   
  15. public class BitmapTest extends Activity{  
  16.   
  17.     String[] images = null;  
  18.     AssetManager assets = null;  
  19.     int currentImge = 0;  
  20.     ImageView image;  
  21.       
  22.     @Override  
  23.     protected void onCreate(Bundle savedInstanceState) {  
  24.         // TODO Auto-generated method stub   
  25.         super.onCreate(savedInstanceState);  
  26.         setContentView(R.layout.bitmaptest);  
  27.           
  28.         image = (ImageView)findViewById(R.id.imageBitmap);  
  29.           
  30.         try{  
  31.             assets = getAssets();  
  32.             //获取/assests/目录下的所有的文件   
  33.             images = assets.list("");  
  34.               
  35.         }catch(IOException e){  
  36.             e.printStackTrace();  
  37.         }  
  38.          
  39.         final Button next = (Button)findViewById(R.id.btnBitmap);  
  40.           
  41.         next.setOnClickListener(new OnClickListener() {  
  42.               
  43.             @Override  
  44.             public void onClick(View v) {  
  45.                 // TODO Auto-generated method stub   
  46.             if(currentImge >= images.length){  
  47.                 currentImge = 0;  
  48.             }  
  49.               
  50.             //找到下一个图片文件   
  51.             while(!images[currentImge].endsWith(".png")){  
  52.                  currentImge++;  
  53.                 //如果发生数组越界   
  54.                 if(currentImge >= images.length){  
  55.                     currentImge = 0;  
  56.                 }  
  57.             }  
  58.   
  59.             InputStream assetFile = null;  
  60.               
  61.             try{  
  62.                 //打开指定资源对应的输入流   
  63.                 assetFile = assets.open(images[currentImge++]);  
  64.       
  65.             }catch(IOException e){  
  66.                 e.printStackTrace();  
  67.             }  
  68.             BitmapDrawable bitmapDrawable = (BitmapDrawable)image.getDrawable();  
  69.             //如果图片还未回收,先强制回收该图片   
  70.             if(bitmapDrawable !=null && !bitmapDrawable.getBitmap().isRecycled()){  
  71.                 bitmapDrawable.getBitmap().recycle();  
  72.             }  
  73.             //该变现实的图片   
  74.             image.setImageBitmap(BitmapFactory.decodeStream(assetFile));  
  75.                   
  76.             }  
  77.         });  
  78.     }  
  79. }  

更多Android相关信息见Android 专题页面 http://www.bkjia.com/topicnews.aspx?tid=11

相关内容