我想阅读带有子目录的移动状态目录的内容以及Android手机中可用的文件。
我已经实现了一个获取Android手机内容的应用程序如下
Intent intent = new Intent();
intent.setAction(Intent.ACTION_PICK);
// Browse in the following folder.
Uri startDir = Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath());
// Files and directories
intent.setDataAndType(startDir, "vnd.android.cursor.dir/lysesoft.andexplorer.file");
我可以在不使用 andexplorer.file 的情况下获取目录和文件。或者有没有办法获取Android手机的内容?
答案 0 :(得分:1)
“获取Android手机的内容”并不完全清楚你的意思。如果您正在讨论文件和目录列表,这里有一个简单的文件浏览器应用程序,它显示了如何:
package com.explorenm.filelist;
import java.io.File;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class FileList extends ListActivity {
private String startdir = "/";
private File workingdir = new File(startdir);
private Context ctx;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ctx = this;
if (workingdir.exists()) {
setListAdapter(new ArrayAdapter<String>(ctx, R.layout.listitem, workingdir.list()));
ListView lv = getListView();
lv.setTextFilterEnabled(true); // allow user to type to go to a specific item in the list
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
File selection = (workingdir.listFiles())[position];
if (selection.isDirectory()) {
workingdir = selection;
setListAdapter(new ArrayAdapter<String>(ctx, R.layout.listitem, workingdir.list()));
} else {
Toast.makeText(getApplicationContext(), selection.getAbsolutePath(), Toast.LENGTH_SHORT).show();
}
}
});
//registerForContextMenu(lv);
} else {
setContentView(R.layout.main);
TextView tv = (TextView) findViewById(R.id.tv);
tv.setText(workingdir.getAbsolutePath() + " does not exist.");
}
}
}
假设应用程序已使该文件可读(或您是root用户的root用户),则可以使用标准Java技术来读取文件内容;如何回答其他问题,例如how to read file from the phone memory in android?