android遍历sd卡中的所有文件

本文介绍了一个简单的Android应用程序,用于从SD卡中选择并上传图片。该应用通过自定义的文件过滤器来查找特定类型的图像文件(如jpg、png等),并在界面上显示这些文件供用户选择。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  最近做个上传图片功能,需要对sd中的所需要的图片进行上传,需要在sd卡中找到这个图片。具体方法如下:

file_row.xml:

   <?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@+id/text1"
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />

directory_list.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@+id/text1"
 xmlns:android="
http://schemas.android.com/apk/res/android"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />

 

 

TestGrid.java:

 

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

import android.app.ListActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ListView;


public class TestGrid   extends ListActivity {
 private static final FileFilter IMAGES_FILTER = new FileFilter() {
 
  public boolean accept(File f) {
   return f.isDirectory() || f.getName().matches("^.*?\\.(jpg|png|bmp|gif)$");
  }
 };

 private FileListAdapter fileList;


 public void onCreate(Bundle icicle) {
  super.onCreate(icicle);
  setContentView(R.layout.directory_list);
  fill(new File("/"));
 }


 public boolean onKeyDown(int keyCode, KeyEvent event) {
  if (keyCode == KeyEvent.KEYCODE_BACK && !fileList.isRoot()) {
   fill(fileList.getItem(1));
   return true;
  }
  return super.onKeyDown(keyCode, event);
 }

 private void fill(File folder) {
  List<File> files = new ArrayList<File>();
  files.add(folder);
  if (folder.getParentFile() != null)
   files.add(folder.getParentFile());

  for (File file : folder.listFiles(IMAGES_FILTER)) {
   files.add(file);
  }

  fileList = new FileListAdapter(this, R.layout.file_row, files);
  setListAdapter(fileList);
 }

 @Override
 protected void onListItemClick(ListView l, View v, int position, long id) {
  File file = fileList.getItem(position);
  if (file.isDirectory())
   fill(file);
  // TODO use image switcher to display selected images
 }
}

 

FileListAdapter.java

 

import java.io.File;
import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class FileListAdapter extends ArrayAdapter<File>{

 public FileListAdapter(Context context, int Resource,List<File> objects) {
  super(context,Resource,objects);
  // TODO Auto-generated constructor stub
 }
 
   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
     TextView view = (TextView) super.getView(position, convertView, parent);
     File file = getItem(position);
     if (position == 0)
       view.setText(view.getResources().getString(R.string.current_folder) + file.getAbsolutePath());
     else if (position == 1 && !isRoot())
       view.setText(R.string.to_parent_folder);
     else
       view.setText(file.getName());
     return view;
   }

   public boolean isRoot() {
     return getItem(0).getParent() == null;
   }

 }

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhchzh1000

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值