问题
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