3

I have made an app in which i am successfully downloading pdf files from internet by URL. And sotre them to the app's internal storage with making a folder app_Pdf. But now I want to open that file with third party app like adobe pdf viewer...ect. I have tried so many way, i have goggling much for this issue. I have also read about making first content provider and serve pdf file by this questing : How to open a PDF stored in Internal Memory but i get the error : invalid path....

Please any buddy help me to solve this issue. It will be good if give me some working code Thank you very much in advance.....

This is how i download and store 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....

Community
  • 1
  • 1
  • "It will be good if give me some working code" -- you were pointed to working code from the question you linked to. Here is that code again: https://github.com/commonsguy/cw-omnibus/tree/master/ContentProvider/Files. It certainly works. – CommonsWare Feb 12 '13 at 12:54
  • yes i have checked it ..i have put FileProvider class in my project and chage URI : public static final Uri CONTENT_URI=Uri.parse("content://com.mydemo.pdf/"); with my package...but still i got error please guide me... – Himanshu Dhakecha Feb 12 '13 at 12:59
  • have u find any thing in my code ?...please replay... – Himanshu Dhakecha Feb 12 '13 at 13:10
  • did you resolve the problem? – Frank Nov 11 '13 at 14:40

0 Answers0