package com.my.sd;
import java.io.File;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import com.drandxq.gallerym.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class testSD extends Activity {
//view
TextView tView ;
ListView i ;
Button btn ;
//data
private ArrayList<File> list ;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Field[] fields = R.drawable.class.getDeclaredFields();
for (Field field : fields)
{
if (field.getName().startsWith(icon))
{
int index = 0;
try {
index = field.getInt(R.drawable.class);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//����ͼƬID
imgList.add(index);
}
}
tView = (TextView)findViewById(R.id.textview1);
//btn = (Button) findViewById(R.id.go);
tView = new TextView(this);
tView.setText("get all files in your sd card");
//init data
i = (ListView) findViewById(R.id.music_list);
list = new ArrayList<File>();
getAllFiles(new File("/sdcard"));
SimpleAdapter ladapter = new SimpleAdapter(this,getMapData(list),R.layout.relative, new String[]{"ItemText","ItemTitle"},new int[]{R.id.ItemTitle,R.id.ItemText});
i.setAdapter(ladapter);
TextView empty = new TextView(this);
empty.setText("empty!");
i.setEmptyView(empty);
}
private ArrayList<Map<String, Object>> getMapData(ArrayList<File> list){
ArrayList<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
HashMap<String,Object> item;
int i = 0 ;
for(i=0;i<list.size();i++){
item = new HashMap<String,Object>();
String path = list.get(i).toString();
String name = path.substring(path.lastIndexOf("/")+1,path.length());
//����ÿһ��list��Ԫ������ ��
item.put("ItemText",name);
item.put("ItemTitle", path);
data.add(item);
}
return data;
}
private void getAllFiles(File root){
File files[] = root.listFiles();
if(files != null)
for(File f:files){
if(f.isDirectory()){
getAllFiles(f);
}
else{
if(f.getName().indexOf(".apk")>0)
this.list.add(f);
}
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
前往页