Android显示SD卡上的图片

Android显示SD卡上的图片 (2012-04-08 21:29:53)转载▼ 标签: 杂谈 分类: Android应用开发


Android应用中显示SD卡上的图片可以使用ImageView,使用BitmapFactory的decodeFile方法读取图片文件,以下为一个最基本的例子:


1. 在main.xml文件中加入组建ImageView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ImageView
android:id="@+id/myView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>

</LinearLayout>

2.JAVA代码:

package csst.picture;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.widget.ImageView;

public class PictureActivity extends Activity {

private ImageView myView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

myView = (ImageView)findViewById(R.id.myView);
String path = Environment.getExternalStorageDirectory() + "/";
String name = path + "wife.jpg";
BitmapFactory.Options option = new BitmapFactory.Options();
option.inSampleSize = 4;
Bitmap bm = BitmapFactory.decodeFile(name,option);
myView.setImageBitmap(bm);

}
}

你可能感兴趣的:(android)