View pdfs stored in assets folder from listview click in fragment

半世苍凉 提交于 2020-07-10 08:13:48

问题


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;

        }
    }
}

回答1:


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.



来源:https://stackoverflow.com/questions/21961050/view-pdfs-stored-in-assets-folder-from-listview-click-in-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!