I created an application in which I successfully download PDF files from the Internet at a URL. And erase them to the internal storage of the application with the creation of the app_Pdf folder. But now I want to open this file with a third-party application such as adobe pdf viewer ... ect. I tried so much, I have many problems for this problem. I also read about how to create the first content provider and execute the pdf file in this quest: How to open a PDF file stored in internal memory , but I get an error message: invalid path ...
Please, buddy help me solve this problem. It would be nice if you give me some working code Thank you very much in advance.
This is how I download and save the file in internal memory:
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); downloadFile("http://www.testdemo.net/testdemo/test/test/test.pdf"); } public boolean downloadFile(String path) { try { URL url = new URL(path); URLConnection ucon = url.openConnection(); ucon.setReadTimeout(5000); ucon.setConnectTimeout(10000); InputStream is = ucon.getInputStream(); BufferedInputStream inStream = new BufferedInputStream(is, 1024 * 5); File file = new File(getDir("Pdf", Context.MODE_WORLD_READABLE) + "/yourfile.pdf"); if (file.exists()) { file.delete(); } file.createNewFile(); FileOutputStream outStream = new FileOutputStream(file); byte[] buff = new byte[5 * 1024]; int len; while ((len = inStream.read(buff)) != -1) { outStream.write(buff, 0, len); } outStream.flush(); outStream.close(); inStream.close(); } catch (Exception e) { e.printStackTrace(); return false; } return true; }
}
and this is how I tried to view pdf from internal storage:
File file = new File(getDir("Pdf", Context.MODE_PRIVATE) + "/yourfile.pdf"); Uri internal = Uri.fromFile(file); viewPdf(internal); private void viewPdf(Uri file) { Intent intent; intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(file, "application/pdf"); try { startActivity(intent); } catch (ActivityNotFoundException e) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("No Application Found"); builder.setMessage("Download one from Android Market?"); builder.setPositiveButton("Yes, Please", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Intent marketIntent = new Intent(Intent.ACTION_VIEW); marketIntent.setData(Uri.parse("market://details?id=com.adobe.reader")); startActivity(marketIntent); } }); builder.setNegativeButton("No, Thanks", null); builder.create().show(); Log.v("","Exception : "+e); }
Please help me....
source share