通过意向Android开放的PDF文件 [英] Android open pdf file via Intent

查看:114
本文介绍了通过意向Android开放的PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SD卡中的某个文件夹的一些PDF文件。我创建了一个应用程序,显示所有PDF作为ListView控件。当我在任何PDF文件双击它,在OfficeSuite应用程序提供的错误(不支持或损坏的文件格式。有什么问题与code,这里是code。

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为显示的ListView项目

//Code for Items displayed as 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;
}

// code打开文件的意向书VIA

//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
    }

错误:

损坏或不支持的文件格式

Corrupt or unsupported file format

推荐答案

我想你忘了指定文件。看到你的code,你只有它指向的文件夹,但没有文件本身。我想,这就是为什么它会告诉你,这是格式错误的,因为它不与.PDF结束

I think you forgot to specify the file. See your code, you only pointed it to the folder, but no the file itself. I think that is why it tells you that it is in wrong format, because it does not end with .pdf

File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "SOMEFOLDER" + File.separator + "pdffile.pdf");

编辑:根据您的意见修改方法

Modify methods according to your comment

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        // Clicking on items
        String fileName = FilesInFolder.get(position);
        open_File(fileName);
    }
});

public void open_File(String filename){
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/SOMEFOLDER", filename);
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
}

这篇关于通过意向Android开放的PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆