使用Bitmap遍历Assets目录下图片

本文介绍了如何在Android应用中遍历Assets文件夹,加载并显示所有图片。通过Bitmap对象和AssetManager,实现对Assets目录下图片资源的读取与展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <Button 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btn"
        android:text="change img"/>

    <ImageView 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/img"/>
</LinearLayout>

java代码:

package com.example.android_test;

import java.io.IOException;
import java.io.InputStream;

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.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class AssetTest extends Activity {

	Button button;
	ImageView image;
	AssetManager manager=null;
	String[] images=null;
	int currentImg=0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.asset_layout);
		
		button=(Button) findViewById(R.id.btn);
		image=(ImageView) findViewById(R.id.img);
			
		try {
			//获取资源
			manager=getAssets();
			//获取该目录下所有文件
			images=manager.list("");
			
			for (int i = 0; i < images.length; i++) {
				System.out.println(images[i]);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				for (;;) {
					
					System.out.println("当前文件:"+images[currentImg]+currentImg);

					//遍历文件为以后缀为".jpg"的文件
					if (images[currentImg].endsWith(".jpg")) {
						
						if (currentImg>=images.length) {
							currentImg=0;
						}
						
						InputStream in=null;
						
						try {
							//打开一个输入流
							in=manager.open(images[currentImg]);
							
						} catch (IOException e) {
							e.printStackTrace();
						}
						
						BitmapDrawable bitmapDrawable=(BitmapDrawable) image.getDrawable();
						//如果图片还未回收,强制回收
						if (bitmapDrawable!=null && !bitmapDrawable.getBitmap().isRecycled()) {
							System.out.println("bitmapDrawable!=null:"+(bitmapDrawable!=null)+"..."+"!bitmapDrawable.getBitmap().isRecycled():"+(!bitmapDrawable.getBitmap().isRecycled()));
							bitmapDrawable.getBitmap().recycle();
						}else {
							System.out.println("空的...");
						}
						
						image.setImageBitmap(BitmapFactory.decodeStream(in));
						currentImg++;
						
						break;
					}else {
						currentImg++;
					}
					
					if (currentImg>=images.length) {
						currentImg=0;
					}					
					
				}
			}
		});		
	}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值