Android 读取sd卡图片并显示

package com.sdcard_read_all;


import java.io.File;
import java.util.ArrayList;


import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;


public class MainActivity extends Activity {
private TextView textView1;
private GridView gridView1;
private static Handler handler;
// 用于存放sdcard卡上的所有图片路径
public static ArrayList<String> dirAllStrArr = new ArrayList<String>();


// 用于遍历sdcard卡上所有文件的类
public static void DirAll(File dirFile) throws Exception {
if (dirFile.exists()) {
File files[] = dirFile.listFiles();
for (File file : files) {
String fileName = file.getName();
String filePath = file.getPath();
Message msg = new Message();
msg.obj = "正在读取:" + filePath;
handler.sendMessage(msg);
if (file.isDirectory()) {


// 除sdcard上Android这个文件夹以外。
if (!fileName.endsWith("Android")) {
// 如果遇到文件夹则递归调用。
DirAll(file);
}
} else {
// 如果是图片文件压入数组


if (fileName.endsWith(".jpg") || fileName.endsWith(".jpeg")
|| fileName.endsWith(".bmp")
|| fileName.endsWith(".gif")
|| fileName.endsWith(".png")) {
// 如果遇到文件则放入数组
if (dirFile.getPath().endsWith(File.separator)) {
dirAllStrArr
.add(dirFile.getPath() + file.getName());
} else {
dirAllStrArr.add(dirFile.getPath() + File.separator
+ file.getName());
}
}
}
}
}
}


// 图片加载的缓存工具类,安卓自带的方法
public static BitmapFactory.Options getHeapOpts(File file) {
BitmapFactory.Options opts = new BitmapFactory.Options();
// 数字越大读出的图片占用的heap必须越小,不然总是溢出
if (file.length() < 20480) { // 0-20k
opts.inSampleSize = 1;// 这里意为缩放的大小 ,数字越多缩放得越厉害
} else if (file.length() < 51200) { // 20-50k
opts.inSampleSize = 2;
} else if (file.length() < 307200) { // 50-300k
opts.inSampleSize = 4;
} else if (file.length() < 819200) { // 300-800k
opts.inSampleSize = 6;
} else if (file.length() < 1048576) { // 800-1024k
opts.inSampleSize = 8;
} else {
opts.inSampleSize = 10;
}
return opts;
}


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1 = (TextView) findViewById(R.id.textView1);
gridView1 = (GridView) findViewById(R.id.gridView1);
gridView1.setVisibility(View.INVISIBLE);
/* 遍历sdcard旗下的所有文件夹开始 */


Thread readSdcard = new Thread() {
private String sdpath = Environment.getExternalStorageDirectory()
.getAbsolutePath();// 获取sdcard的根路径
private File dirFile = new File(sdpath);


public void run() {
try {
DirAll(dirFile);
Message msg = new Message();
msg.obj = "0";
handler.sendMessage(msg);
} catch (Exception e) {
e.printStackTrace();
}
};
};
textView1.setVisibility(View.VISIBLE);
readSdcard.start();


/* 遍历sdcard旗下的所有文件夹结束 */
// 不停在接受定时器的消息,根据消息的参数,进行处理


handler = new Handler(new Handler.Callback() {// 这样写,就不弹出什么泄漏的警告了
@Override
public boolean handleMessage(Message msg) {
textView1.setText(msg.obj + "");
if (msg.obj.equals("0")) {
textView1.setVisibility(View.GONE);
gridView1.setVisibility(View.VISIBLE);
}
return false;
}
});


BaseAdapter baseAdapter = new BaseAdapter() {


@Override
public View getView(int position, View convertView, ViewGroup arg2) {
ImageView imageView1;
if (convertView == null) {
imageView1 = new ImageView(MainActivity.this);
imageView1.setAdjustViewBounds(true);// 自动缩放为宽高比
imageView1.setScaleType(ScaleType.CENTER_INSIDE);// 设置图片保持宽高比显示
imageView1.setPadding(5, 5, 5, 5);
} else {
imageView1 = (ImageView) convertView;
}
String filePath = dirAllStrArr.get(position);
File file = new File(filePath);
Bitmap bm = BitmapFactory.decodeFile(filePath,
getHeapOpts(file));
imageView1.setImageBitmap(bm);


return imageView1;
}


// 获取当前选项
@Override
public long getItemId(int position) {
return position;
}


@Override
public Object getItem(int position) {
return position;
}


// 获取数量
@Override
public int getCount() {
return dirAllStrArr.size();
}
};
gridView1.setAdapter(baseAdapter);// 把适配器与网格视图链接起来
gridView1.setOnItemClickListener(new OnItemClickListener() {// 点击网格组件的任意一张图片时候的事件
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position,// position为点击的id
long arg3) {
Intent intent = new Intent(MainActivity.this,
ViewActivity.class);// 激活ViewActivity
Bundle bundle = new Bundle();
bundle.putString("imgPath", dirAllStrArr.get(position));// 传递点击的图片的id到ViewActivity
intent.putExtras(bundle);
startActivity(intent);
}
});
}


// 创建menu的方法,没有该方法,不会在右上角设置菜单。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// 设置menu界面为res\menu\menu.xml
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


// 处理菜单事件
public boolean onOptionsItemSelected(MenuItem item) {
// 得到当前选中的MenuItem的ID,
int item_id = item.getItemId();
switch (item_id) {
// 设置id为menu_exit的菜单子项所要执行的方法。
case R.id.menu_exit:
System.exit(0);// 结束程序
break;
}
return true;
}


// 对物理按钮的监听
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
finish();// 关闭这个Activity。
break;
}
return super.onKeyDown(keyCode, event);
}


}





ackage com.sdcard_read_all;


import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;


public class ViewActivity extends Activity {
private TextView textView1;
private ImageView imageView1;
private Button button1;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
textView1 = (TextView) findViewById(R.id.textView1);
imageView1 = (ImageView) findViewById(R.id.imageView1);
button1 = (Button) findViewById(R.id.button1);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String imgPath = bundle.getString("imgPath");
textView1.setText("图片路径:" + imgPath);
Bitmap bm = BitmapFactory.decodeFile(imgPath);
imageView1.setImageBitmap(bm);
button1.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View arg0) {
finish();
}
});


}


// 对物理按钮的监听
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
finish();// 关闭这个Activity。
break;
}
return super.onKeyDown(keyCode, event);
}
}

没有更多推荐了,返回首页