I am trying to download a pdf from the internet, store the file in internal storage and then launch the pdf in an intent. I keep getting "The document is empty (size 0KB)" when it launches the intent.

class Pdf extends AsyncTask<Void, Void, String>{

     private Context ctx;

      public Pdf(Context ctx){
          this.ctx = ctx;

      }

    @Override
    protected String doInBackground(Void... params) {

         try {

             FileOutputStream f = ctx.openFileOutput("city.pdf", ctx.MODE_WORLD_READABLE);
             URL u = new URL("http://www.example.com/city.pdf");

             HttpURLConnection c = (HttpURLConnection) u.openConnection();
             c.setRequestMethod("GET");
             c.setDoOutput(true);
             c.connect();
             InputStream in = c.getInputStream();
             byte[] buffer = new byte[1024];
             int len1 = 0;
             while ((len1 = in.read(buffer)) > 0) {
                     f.write(buffer, 0, len1);
             }
             f.close();
     } catch (Exception e) {
             e.printStackTrace();
     }



         File file = new File(ctx.getFilesDir(), "city.pdf");
         PackageManager packageManager = ctx.getPackageManager();
         Intent testIntent = new Intent(Intent.ACTION_VIEW);
         testIntent.setType("application/pdf");
         List list = packageManager.queryIntentActivities(testIntent,
                 PackageManager.MATCH_DEFAULT_ONLY);
         Intent intent = new Intent();
         intent.setAction(Intent.ACTION_VIEW);
         Uri uri = Uri.fromFile(file);
         intent.setDataAndType(uri, "application/pdf");
         ctx.startActivity(intent);


        return null;
    }

}

This has been driving me crazy, any ideas?

有帮助吗?

解决方案

You will not be able to do this from the filesDir. I had run into the same issue.

You can do the following:

private void playFile(String fileName)
{
    try
    {
        File documentFile = new File(fileName);

        String externalStroageString = appContext.getExternalCacheDir()+"/Download/";
        File externalStroage = new File(externalStroageString);

        File documentFileExternal = new File(externalStroage, documentFile.getName());
        if(documentFileExternal.exists())
            documentFileExternal.delete();
        try 
        {
            FileUtils.copyFile(documentFile, documentFileExternal);
            documentFileExternal.setReadable(true, false);

        } 
        catch (IOException e) 
        {

        }
        MimeTypeMap map = MimeTypeMap.getSingleton();
        int index = documentFileExternal.getName().lastIndexOf(".");
        String ext = documentFileExternal.getName().substring(index+1);
        String type = map.getMimeTypeFromExtension(ext);
        Uri uri = Uri.parse("file://"+documentFileExternal.getAbsolutePath());
        if (type == null)
            type = "*/*";

        try
        {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            if(ext.equalsIgnoreCase("htm") || ext.equalsIgnoreCase("html"))
            {
                intent.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
                intent.addCategory(Intent.CATEGORY_BROWSABLE);
            }

            intent.setDataAndType(uri, type);

            appContext.startActivity(intent);
        }

        catch(Exception ex)
        {
            ErrorManager.appContext = appContext;
            ErrorManager.errorCode = "UnsupportedFileType";
            ErrorManager.displayErrorDialog();
        }
    }

    catch(Exception ex)
    {}
} 
catch(Exception ex)
{

}

}

其他提示

Try

FileInputStream inputStream = new FileInputStream(ctx.openFileInput("city.pdf"),ctx.MODE_WORLD_READABLE);

for retrieving your file. Did not test this...

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top