Android 下载 pdf 到内部存储并启动 intent

标签 android pdf android-intent androidhttpclient

我正在尝试从 Internet 下载 pdf,将文件存储在内部存储器中,然后在 intent 中启动 pdf。启动 Intent 时,我不断收到“文档为空(大小 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;
    }

}

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

最佳答案

您将无法从 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)
{

}

关于Android 下载 pdf 到内部存储并启动 intent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22170307/

相关文章:

java - 打开 pdf 时出现 MUPDF UnsupportedOperationException

c++ - 从 (La)TeX 创建 PDF

android - 为什么 PendingIntent 不发回我的 Intent 自定义 Extras 设置?

android - 在 Activity 之间传递 Drawable

java - 在 Android 中禁用快速设置磁贴

android - 如何在android中的ScrollView上设置Gesture的onFling()事件?

pdf - 如何旋转文档页面以横向显示?

android - 将 PDF 加载到 Android webview 中

android - ACTION_EDIT 和 ACTION_VIEW 有什么区别?

java - 使用okhttp上传文件