【问题标题】:Android open pdf file via IntentAndroid通过Intent打开pdf文件
【发布时间】: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


【解决方案1】:

我想你忘记指定文件了。查看您的代码,您仅将其指向文件夹,但没有指向文件本身。我认为这就是为什么它告诉你它的格式错误,因为它不以 .pdf 结尾

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

编辑:根据您的评论修改方法

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
}

【讨论】:

  • SOMEFOLDER 有很多 pdf。我创建了一个 ListView,其中所有这些 pdf 都是可见的。我需要通过单击打开那个特定的 pdf。那么在那种情况下我怎么能指定一个特定的文件。
  • 指定一个特定的文件名将导致同一个文件一次又一次地打开。但就我而言,我需要打开我点击的那个文件
  • 是的,我已经修改了我的答案。您需要稍微修改方法,您需要获取您单击的文件的名称并将其传递给打开文件功能。查看我的编辑
  • 非常感谢。你拯救了我的一天。
猜你喜欢
  • 2012-09-17
  • 1970-01-01
  • 2012-05-18
  • 2020-12-02
  • 2014-06-08
  • 1970-01-01
  • 2015-10-18
  • 2019-05-13
  • 2012-07-10
相关资源
最近更新 更多