【发布时间】:2015-05-23 16:51:20
【问题描述】:
我在 sdcard 的某个文件夹中有一些 pdf 文件。我创建了一个将所有 pdf 显示为 ListView 的应用程序。当我单击任何 pdf 文件时,OfficeSuite 应用程序中出现错误(不支持或文件格式损坏。代码有问题。这是代码。
//显示为ListVIew的项目代码
ListView lv;
ArrayList<String> FilesInFolder = GetFiles(Environment.getExternalStorageDirectory()
+ "/SOMEFOLDER");
lv = (ListView) findViewById(R.id.filelist);
lv.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, FilesInFolder));
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// Clicking on items
open_File();
}
});
public ArrayList<String> GetFiles(String DirectoryPath) {
ArrayList<String> MyReports = new ArrayList<String>();
File f = new File(DirectoryPath);
f.mkdirs();
File[] files = f.listFiles();
if (files.length == 0)
return null;
else {
for (int i=0; i<files.length; i++)
MyReports.add(files[i].getName());
}
return MyReports;
}
//VIA Intent打开文件的代码
public void open_File(){
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/SOMEFOLDER");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Intent intent1 = Intent.createChooser(intent, "Open With");
try {
startActivity(intent1);
} catch (ActivityNotFoundException e) {
// Instruct the user to install a PDF reader here, or something
}
错误:
文件格式损坏或不受支持
【问题讨论】:
-
您是否尝试从文件资源管理器中打开它们,是否有效?
-
是的,它从 OfficeSuite 应用程序中打开。
标签: android pdf android-intent