我在文件夹中列出PDF文件。但是我希望它们被命名为文件名,而不是sdcard / mypath / files。另外,每当我通过PDF查看器点击它们时我想打开它们。我的代码:
public class activity1 extends ListActivity {
private List<String> fileList = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
File root = new File("sdcard/mypath");
ListDir(root);
}
void ListDir(File f) {
File[] files = f.listFiles();
fileList.clear();
for (File file : files) {
fileList.add(file.getPath());
}
ArrayAdapter<String> directoryList = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, fileList);
setListAdapter(directoryList);
}}
答案 0 :(得分:0)
public class activity1 extends ListActivity {
ListView lv;
private List<String> fileList = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
File root = new File("sdcard/mypath");
lv = getListView();
ListDir(root);
}
void ListDir(File f) {
File[] files = f.listFiles();
fileList.clear();
for (File file : files) {
fileList.add(file.getName());
}
ArrayAdapter<String> directoryList = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, fileList);
setListAdapter(directoryList);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
viewPdf(fileList.get(i));
}
});
}
}}
并以pdf默认pdf查看器
打开文件 private void viewPdf(String file) {
File pdfFile = new File(Environment.getExternalStorageDirectory() + "/" + "mypath"+ "/" + file);
Uri path = Uri.fromFile(pdfFile);
// Setting the intent for pdf reader
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(pdfIntent);
}
使用以上代码.. 希望它有所帮助!