[Android实例] 获取sd卡图片显示出来
package com.filemanag;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
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.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class Picture extends Activity {
private ArrayList<HashMap<String, Object>> collection;
private GridView gridView=null;
private Bitmap bit;
String imageName;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.music_layout);
this.gridView = (GridView)(findViewById(R.id.myGrid));
collection= getAllMp3FileDataSource();
gridView.setAdapter(new ImageAdapter(this,collection));
this.gridView.setOnItemClickListener(new OnItemClickListenerList());
}
public class ImageAdapter extends BaseAdapter{
private Context context;
private ArrayList<HashMap<String,Object>>data;
public ImageAdapter(Context context,ArrayList<HashMap<String,Object>>data){
this.context=context;
this.data=data;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return this.data.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return this.data.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = View.inflate(Picture.this, R.layout.grid_row_layout, null);
LinearLayout rl = (LinearLayout)view.findViewById(R.id.gridimageLayout);
ImageView image = (ImageView)rl.findViewById(R.id.imageGrid);
image.setImageBitmap(bit);
TextView tv1 = (TextView)rl.findViewById(R.id.TextGrid);
tv1.setText(imageName);
return rl;
}
}
private class OnItemClickListenerList implements OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
HashMap<String, Object> item = (HashMap<String, Object>)Picture.this.collection
.get(arg2);
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
//intent.setType(“audio/*”);
File f = new File(item.get(“Folder”) + “//”
+ item.get(“FileName”).toString());
//Log.e(“Tag”, item.get(“Folder”) + “//” + item.get(“FileName”));
String type = FileUtil.getMIMEType(f.getName());
intent.setDataAndType(Uri.fromFile(f), type);
startActivity(intent);
// startActivity(Intent.createChooser(intent, getTitle()));
}
}
private ArrayList<HashMap<String, Object>> getAllMp3FileDataSource() {
collection= new ArrayList<HashMap<String, Object>>();
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
// 获取sd卡所在目录
File sdRootFolder = Environment.getExternalStorageDirectory();
// 遍历查找所有mp3文件
FindFiles(sdRootFolder);
}
return collection;
}
private void FindFiles(File Folder) {
for (File item : Folder.listFiles()) {
if (item.getName().toLowerCase().endsWith(“.jpg”)) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put(“FileName”, item.getName());
map.put(“Folder”, item.getParent());
bit=BitmapFactory.decodeFile(“Folder”);
collection.add(map);
}
if (item.isDirectory()) {
FindFiles(item);
}
}
}
}