数据要显示的是居住在特定的位置在SD卡中的文件名。是否有可能在列表视图格式来显示呢?请指导我。我如何列出SD卡
Q
我如何列出SD卡
0
A
回答
1
这是东西,
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File("/sdcard/");
// or you can use File f=new File(Environment.getExternalStorageDirectory().getPAth());
File[] files = f.listFiles();
for(int i=0; i < files.length; i++)
{
File file = files[i];
path.add(file.getPath());
if(file.isDirectory())
item.add(file.getName() + "/");
else
item.add(file.getName());
}
ArrayAdapter<String> fileList =
new ArrayAdapter<String>(this, R.layout.row, item);
setListAdapter(fileList);
0
如果你知道的目录从那里,你想从文件的位置/路径,那么你可以使用下面的代码来获取文件的列表,并在列表视图中显示它们。
//Assuming that MEDIA_PATH is the location from where the files are to be retrieved.
List<String> songs=new ArrayList<String>();
File home = new File(MEDIA_PATH); // Getting file representation of the directory
File[] musicFiles=home.listFiles();
if(musicFiles.length>0){
for(File f:musicFiles) { songs.add(f.getName()); }
ListAdapter adapter= new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,songs);
setListAdapter(adapter);
}
如果您正在使用ListActivity
,你就不需要使用setContentView(...)
。
您还可以使用的FilenameFilter,如果你想只有特定类型的文件(如MP3文件)来读取和显示。
0
使用此代码,
public class RecordedVideos extends ListActivity {
private List<String> item = null;
private List<String> path = null;
private String root="/sdcard/Recorded Videos";
private TextView myPath;
Button back;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.filelist);
back=(Button)findViewById(R.id.back);
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
File file = new File(path.get(arg2));
boolean b=file.delete();
return false;
}
});
myPath = (TextView)findViewById(R.id.path);
getDir(root);
back.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
private void getDir(String dirPath)
{
myPath.setText("Location: " + dirPath);
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File(dirPath);
File[] files = f.listFiles();
if(!dirPath.equals(root))
{
item.add(root);
path.add(root);
item.add("..../");
path.add(f.getParent());
}
for(int i=0; i < files.length; i++)
{
File file = files[i];
path.add(file.getPath());
if(file.isDirectory())
item.add(file.getName() + "/");
else
item.add(file.getName());
}
ArrayAdapter<String> fileList =
new ArrayAdapter<String>(this, R.layout.row, item);
setListAdapter(fileList);
}
protected void onListItemClick(ListView l, View v, int position, long id) {
File file = new File(path.get(position));
if (file.isDirectory())
{
if(file.canRead())
getDir(path.get(position));
else
{
new AlertDialog.Builder(this)
.setIcon(R.drawable.icon)
.setTitle("[" + file.getName() + "] folder can't be read!")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).show();
}
}
else
{
/*new AlertDialog.Builder(this)
.setIcon(R.drawable.icon)
.setTitle("[" + file.getName() + "]")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).show(); */
Intent intent= new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri filetype= Uri.parse("file://" + file.getPath());
String filename=file.getName();
if(filename.endsWith(".txt")||filename.endsWith(".doc"))
{
intent.setDataAndType(filetype, "text/*");
startActivity(intent);
}
else if(filename.endsWith(".gif")||filename.endsWith(".jpg")||filename.endsWith(".png"))
{
intent.setDataAndType(filetype, "image/*");
startActivity(intent);
}
else if(filename.endsWith(".mp4")||filename.endsWith(".3gp"))
{
intent.setDataAndType(filetype, "video/*");
startActivity(intent);
}
else
{
intent.setDataAndType(filetype, "audio/*");
startActivity(intent);
}
}
}
}
相关问题
- 1. 如何在SD卡中列出文件?
- 2. 如何SD卡
- 3. 列表从SD卡
- 4. 我如何解析SD卡xml?
- 5. 问题SD卡在SD卡
- 6. 如何“锁定”设备,取出SIM卡或SD卡?
- 7. SD卡:如何强制内核不删除SD卡
- 8. 写入SD卡
- 9. UART到SD卡
- 10. 从SD卡
- 11. 搜索SD卡
- 12. 写入SD卡
- 13. Android的SD卡
- 14. 写入SD卡
- 15. SD卡目录
- 16. 从SD卡
- 17. 如果我有两个物理SD卡“左用户SD卡”和“右用户SD卡”(如vold.fstab中给出的),那么应用程序如何像filemanger一样,知道如何在应用程序中检测左右SD卡
- 18. 如何列出位于SD卡上的所有文件和文件夹
- 19. 从SD卡中取出图像,压缩并保存回SD?
- 20. 2.0设备上出现“SD卡”权限?
- 21. 如何创建虚拟(内存)SD卡?
- 22. Android:如何从SD卡上传图像
- 23. 如何从SD卡中删除文件?
- 24. 如何检查SD卡插入Android?
- 25. 如何删除SD卡中的文件?
- 26. 如何在Android SD卡上写
- 27. 如何添加SD卡图像到coverflow?
- 28. Android:如何:从SD卡显示图像?
- 29. 如何拍摄并存入SD卡?
- 30. 如何清除闪存(SD卡)?