安卓加载assets php,Android_Android读取assets目录下的所有图片并显示的方法,本文实例讲述了Android读取assets - phpStudy...

Android读取assets目录下的所有图片并显示的方法

本文实例讲述了Android读取assets目录下的所有图片并显示的方法。分享给大家供大家参考。具体方法分析如下:

在assets文件夹里面的文件都是保持原始的文件格式,需要用AssetManager以字节流的形式读取文件。

1. 先在Activity里面调用getAssets() 来获取AssetManager引用。

2. 再用AssetManager的open(String fileName, int accessMode) 方法则指定读取的文件以及访问模式就能得到输入流InputStream。

3. 然后就是用已经open file 的inputStream读取文件,读取完成后记得inputStream.close() 。

4.调用AssetManager.close() 关闭AssetManager。

需要注意的是,来自Resources和Assets 中的文件只可以读取而不能进行写的操作。

下面看一下在Activity中使用的示例代码:

List> cateList = new ArrayList>();

String[] list_image = null;

try {

//得到assets/processedimages/目录下的所有文件的文件名,以便后面打开操作时使用

list_image = context.getAssets().list("processedimages");

} catch (IOException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

for(int i=0;i

{

InputStream open = null;

try {

String temp = "processedimages/"+list_image[i];

open = context.getAssets().open(temp);

Bitmap bitmap = BitmapFactory.decodeStream(open);

Map map = new HashMap();

map.put("name", list_image[i]);

map.put("iv", bitmap);

map.put("bg", R.drawable.phone_vip_yes);

map.put("cate_id",i);

cateList.add(map);

// Assign the bitmap to an ImageView in this layout

} catch (IOException e) {

e.printStackTrace();

} finally {

if (open != null) {

try {

open.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

这样所有的map中的关键字“iv"处理论上就保存了我们读取的bitmap,可以结果并非如此,大家应该注意到了在”bg“关键字处我们也保存了一个图片,只不过它是通过R.drawable.方式获取的,实验证明这种方式是可以成功读取并显示的。为什么从assets中读取的bitmap不能显示呢?

解决办法是:

实现 ViewBinder接口,对两种的资源id和bitmap 情况进行说明:

adapter.setViewBinder(new ViewBinder() {

@Override

public boolean setViewValue(

View view,

Object data,

String textRepresentation) {

// TODO Auto-generated method stub

if((view instanceof ImageView) && (data instanceof Bitmap)) {

ImageView imageView = (ImageView) view;

Bitmap bmp = (Bitmap) data;

imageView.setImageBitmap(bmp);

return true;

}

return false;

}

});

这样就可以了。

还有一种情况是,我们在非Activity类中读取assets文件下的内容,这个时候就得把调用者(Activity类)的context传递过去,然后在这个非Activity类中使用context.getAssets()方式调用就行了。

举个简单例子:

我们有一个HomeActivity,然后我们它里面调用GetData.initdata(HomeActivity.this).

在GetData类的initdata方法肯定是这样定义的:

public void initdata(Context context)

{

//other codes...

String[] list_image = null;

try {

//得到assets/processedimages/目录下的所有文件的文件名,以便后面打开操作时使用

list_image = context.getAssets().list("processedimages");//attention this line

} catch (IOException e1)

{

e1.printStackTrace();

}

//other codes.....

}

因为getAssets方法是Context下的方法,在非Activity类中是不能直接使用的。

希望本文所述对大家的Android程序设计有所帮助。相关阅读:

PHP框架Laravel插件Pagination实现自定义分页

Android继承现有控件拓展实现自定义控件textView

Laravel 5.3 学习笔记之 安装

html5教程实现Photoshop渐变色效果

ORA-00392ORA-00312日志正在清除故障

最后的预览版Windows 10 10138:RTM前最后一击

Windows7通过SFC命令修复受损系统文件的具体步骤

JavaScript+CSS实现的可折叠二级菜单实例

kindeditor修复会替换script内容的问题

JDK 7U15在 Windows x86平台下的安装方法

Bootstrap自定义文件上传下载样式

Android中设置只有程序第一次运行才显示的界面实现思路

深入理解JavaScript内置函数

从零开始学android小示例程序

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值