从列表视图中查看存储在资产文件夹中的pdf单击片段 [英] View pdfs stored in assets folder from listview click in fragment

查看:83
本文介绍了从列表视图中查看存储在资产文件夹中的pdf单击片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 ListView ,我希望每个项目都打开位于Android应用程序内 assets/PDFS 文件夹中的某个.pdf文件.以下代码不会引发任何错误,但也不会打开文件.有人可以分享解决方案的想法吗?

I have created a ListView in which I would like each item to open a certain .pdf file located in assets/PDFS folder located within my Android App. The following code does not throw any errors, but does not open the file either. Can someone share an idea for the solution?

请注意,我正在使用片段

Note that I am using Fragment

public class FreshFragment extends Fragment implements OnItemClickListener {

    ListView lv;
    String[] titles;

    public FreshFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View V = inflater.inflate(R.layout.fragment_fresh, container, false);


        lv = (ListView)V.findViewById(android.R.id.list);
        lv.setOnItemClickListener(this);

        return V;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        switch (position) {
        case 0:
            File pdfFile = new File("res/assets/peedee.pdf");
            Uri path = Uri.fromFile(pdfFile);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(path, "application/pdf");
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            break;

        }
    }
}

推荐答案

您无法在其他应用程序中打开资产中的文件.您需要将其写入磁盘,这两个应用程序都可以先读取该磁盘,例如外部存储.

You can't open a file in your assets in another app. You need to write it to disk somewhere both apps can read it first, such as external storage.

这篇关于从列表视图中查看存储在资产文件夹中的pdf单击片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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