android open pdf,Android open pdf file via Intent

这篇博客讨论了一个Android应用程序尝试打开SD卡上某个文件夹中的PDF文件时遇到的问题。当点击列表视图中的PDF文件时,OfficeSuite应用返回错误‘UNSUPPORTED_OR_CORRUPT_FILE_FORMAT’。博主分享了用于显示PDF文件列表的代码,并提供了读取SD卡文件夹中文件的方法。问题可能出在文件路径、文件本身或打开文件的 Intent 设置上。
摘要由CSDN通过智能技术生成

I have some pdf files in some folder in sdcard. I created an app that shows all pdf as ListView. When i click on any pdf file it gives error in OfficeSuite application (UNSUPPORTED OR CORRUPT FILE FORMAT. Is something wrong with the code. Here is the code.

//Code for Items displayed as ListVIew

ListView lv;

ArrayList FilesInFolder = GetFiles(Environment.getExternalStorageDirectory()

+ "/SOMEFOLDER");

lv = (ListView) findViewById(R.id.filelist);

lv.setAdapter(new ArrayAdapter(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 GetFiles(String DirectoryPath) {

ArrayList MyReports = new ArrayList();

File f = new File(DirectoryPath);

f.mkdirs();

File[] files = f.listFiles();

if (files.length == 0)

return null;

else {

for (int i=0; i

MyReports.add(files[i].getName());

}

return MyReports;

}

//Code for opening files 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

}

Error:

Corrupt or unsupported file format

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值