1

我正在尝试从 Internet 下载 pdf,将文件存储在内部存储中,然后有意启动 pdf。启动意图时,我不断收到“文档为空(大小为 0KB)”。

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;
    }

}

这一直让我发疯,有什么想法吗?

4

2 回答 2

1

您将无法从filesDir执行此操作。我遇到了同样的问题。

您可以执行以下操作:

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)
{

}

}

于 2014-03-04T11:21:50.530 回答
0

尝试

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

用于检索您的文件。这个没测试。。。

于 2014-03-04T11:14:32.157 回答