У меня есть несколько файлов в формате pdf в некоторой папке на SD-карте. Я создал приложение, которое отображает все pdf как ListView. Когда я нажимаю на любой файл в формате PDF дает ошибку в приложении OfficeSuite (UNSUPPORTED испорченного или ФОРМАТ. Что-то не так с кодом. Вот код.Android открыть pdf-файл с помощью Intent
// Код для элементов, отображаемых в 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
}
Ошибка:
формат Коррумпированные или неподдерживаемый файл
Вы пытались их открыть из файла explorer, он работает? –
Да, из приложения OfficeSuite он открывается. –