import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;
import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Vector;
public class Main2Activity extends AppCompatActivity {
private ArrayList<File> lists = new ArrayList<File>();
private Vector<File> filelists = new Vector<File>();
public File getSDPath(){
File sdDir = null;
boolean sdCardExist = Environment.getExternalStorageState()
.equals(android.os.Environment.MEDIA_MOUNTED); //判断sd卡是否存在
if (sdCardExist)
{
sdDir = Environment.getExternalStorageDirectory();//获取跟目录
}
return sdDir;
}
public void scanFile(File rootPath, final String filterName) {
rootPath.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
// TODO Auto-generated method stub
if (((pathname + "").toLowerCase()).endsWith(filterName)) {
lists.add(pathname);
return true;
}
if (pathname.isDirectory()) {//如果是目录
scanFile(pathname, filterName);
return true;
} else {
return false;
}
}
});
}
public void scanFile(File rootPath, final String[] filterNames) {
for (int i = 0; i < filterNames.length; i++) {
scanFile(rootPath, filterNames[i]);
}
}
public ArrayList<File> getFilelists() {
return lists;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
/*
Intent intent=getIntent();
String txtname=intent.getStringExtra("txtname");
TextView text=(TextView)findViewById(R.id.textView);
text.setText(txtname);
*/
TextView text2=(TextView)findViewById(R.id.textView2);
text2.setText(getSDPath().toString());
final String[] filterNames = {"txt", ".txt"};
scanFile(getSDPath(), filterNames);
for(int i=0;i<lists.size();i++)
{
i++;
text2.setText(lists.get(i).toString());
}
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
}
android扫描sd卡下的所有.txt 并获取路径
最新推荐文章于 2024-04-28 07:30:38 发布