Android中使用assets下的资源——图片资源
2014-09-03 16:36
357 查看
在Android 应用中使用assets目录下存放的资源文件,assets目录下存放的资源代表应用无法直接访问的原生资源,应用程序通过AssetManager以二进制流的形式来读取资源。此应用是查看/assets/目录下的图片查看器(图片格式为:.png),在assets目录下放几张PNG格式的图片
该程序的界面十分简单,只包含一个ImageView和一个按钮
代码如下:
布局文件如下:bitmaptest.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/btnBitmap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TestBitmap"
/>
<ImageView
android:id="@+id/imageBitmap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
package com.infy.configuration;
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 BitmapTest extends Activity{
String[] images = null;
AssetManager assets = null;
int currentImge = 0;
ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.bitmaptest);
image = (ImageView)findViewById(R.id.imageBitmap);
try{
assets = getAssets();
//获取/assests/目录下的所有的文件
images = assets.list("");
}catch(IOException e){
e.printStackTrace();
}
final Button next = (Button)findViewById(R.id.btnBitmap);
next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(currentImge >= images.length){
currentImge = 0;
}
//找到下一个图片文件
while(!images[currentImge].endsWith(".png")){
currentImge++;
//如果发生数组越界
if(currentImge >= images.length){
currentImge = 0;
}
}
InputStream assetFile = null;
try{
//打开指定资源对应的输入流
assetFile = assets.open(images[currentImge++]);
}catch(IOException e){
e.printStackTrace();
}
BitmapDrawable bitmapDrawable = (BitmapDrawable)image.getDrawable();
//如果图片还未回收,先强制回收该图片
if(bitmapDrawable !=null && !bitmapDrawable.getBitmap().isRecycled()){
bitmapDrawable.getBitmap().recycle();
}
//该变现实的图片
image.setImageBitmap(BitmapFactory.decodeStream(assetFile));
}
});
}
}
在Android 应用中使用assets目录下存放的资源文件,assets目录下存放的资源代表应用无法直接访问的原生资源,应用程序通过AssetManager以二进制流的形式来读取资源。此应用是查看/assets/目录下的图片查看器(图片格式为:.png),在assets目录下放几张PNG格式的图片
该程序的界面十分简单,只包含一个ImageView和一个按钮
代码如下:
布局文件如下:bitmaptest.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/btnBitmap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TestBitmap"
/>
<ImageView
android:id="@+id/imageBitmap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
package com.infy.configuration;
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 BitmapTest extends Activity{
String[] images = null;
AssetManager assets = null;
int currentImge = 0;
ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.bitmaptest);
image = (ImageView)findViewById(R.id.imageBitmap);
try{
assets = getAssets();
//获取/assests/目录下的所有的文件
images = assets.list("");
}catch(IOException e){
e.printStackTrace();
}
final Button next = (Button)findViewById(R.id.btnBitmap);
next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(currentImge >= images.length){
currentImge = 0;
}
//找到下一个图片文件
while(!images[currentImge].endsWith(".png")){
currentImge++;
//如果发生数组越界
if(currentImge >= images.length){
currentImge = 0;
}
}
InputStream assetFile = null;
try{
//打开指定资源对应的输入流
assetFile = assets.open(images[currentImge++]);
}catch(IOException e){
e.printStackTrace();
}
BitmapDrawable bitmapDrawable = (BitmapDrawable)image.getDrawable();
//如果图片还未回收,先强制回收该图片
if(bitmapDrawable !=null && !bitmapDrawable.getBitmap().isRecycled()){
bitmapDrawable.getBitmap().recycle();
}
//该变现实的图片
image.setImageBitmap(BitmapFactory.decodeStream(assetFile));
}
});
}
}
相关文章推荐
- Android 下发或Assets 使用.9图解决图片资源适配问题
- Android中使用assets下的资源——图片资源
- android-读取Assets图片资源保存到SD - 随心
- Android资源文件夹res/raw和assets的使用
- 【Android】使用系统相机或者从相册中获取图片资源并进行适当裁剪
- 使用tinypng优化Android的资源图片
- Android从assets中读取文本和图片资源
- 如何在使用eclipse的情况下,清理android项目中的冗余class文件和资源文件以及冗余图片
- Android ImageView 使用assets里的图片
- Android 获取assets文件夹中的图片资源
- Android解决图片资源比较大的问题--->.9图片的制作和使用
- 在Android Jar包使用图片资源的解决方法
- android自定义进度条渐变色View,不使用任何图片资源
- Android 将Activity打成jar包 供第三方使用 彻底解决XML 图片 文字资源不可打入到jar包的问题!
- Android图片资源的使用
- android-读取Assets图片资源保存到SD - 随心
- android adt使用外部图片资源
- android 使用canvas修改资源里的图片
- Android Jar包中使用图片资源
- android--assets目录下资源使用总结