如何通过在Android中指定文件夹路径移动到外部SD卡中的文件夹
请帮助,当通过指定文件夹的完整路径单击按钮时,如何显示指定文件夹的内容。我尝试如下,但我的应用程序崩溃了。
Button buttonFom = (Button) findViewById(R.id.fom);
buttonFom.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
getDir("/mnt/sdcard/external_sd/DOC1/HW 850Xp/");
}
});
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);
}
Please help, how to display the specified folder's contents, when click on a button by specifying the folder's full path.I tried like below, but My application crashes.
Button buttonFom = (Button) findViewById(R.id.fom);
buttonFom.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
getDir("/mnt/sdcard/external_sd/DOC1/HW 850Xp/");
}
});
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);
}
首先,您需要在 Android Manifest 文件中设置用户权限以写入访问 SD 卡的权限,
如果没有它,您将无法访问 SD 卡
希望,这对您有帮助。
First you need to set user-permission to write access to SD card in Android Manifest file
Without it you can't access to SD card
Hope, it's help you.