【问题标题】:How can I read the file after the download and display in pdf viewer in android下载后如何读取文件并在android的pdf查看器中显示
【发布时间】:2019-01-06 13:16:18
【问题描述】:

我创建了一个应用程序,如果用户点击任何 PDF,它就会下载该 PDF。现在我想在下载后显示该 PDF。我不知道如何在 onPostExecute 方法下载后显示该 pdf。文件存储路径为Environment.getExternalStorageDirectory() + "/" + "android" + "/" + "Data" + "/"+ "foldername"+"/"+"Filename"

public class DownloadTask {
cutomealertbox cdd;
File apkStorage = null;
File outputFile = null;
private Context context;

private String downloadUrl = "", downloadFileName = "";
public DownloadTask(Context context, String downloadUrl) {
    this.context = context;

    this.downloadUrl = downloadUrl;
    downloadFileName = downloadUrl.substring(downloadUrl.lastIndexOf('/'), downloadUrl.length());
    Log.e(TAG, downloadFileName);
    cdd=new cutomealertbox((Activity) context);
    new DownloadingTask().execute();
}

private class DownloadingTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        cdd.show(); // cdd is alert box
    }

    @Override
    protected void onPostExecute(Void result) {
        try {
            if (outputFile != null) {
                cdd.dismiss();
            } else {

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {

                    }
                }, 3000);

                Log.e(TAG, "Cannot load please try again");

            }
        } catch (Exception e) {
                Log.e(TAG, "Download Failed with Exception - " + e);

        }

【问题讨论】:

  • 看看this
  • @Ajeet Jain 您使用哪个库进行 PDF 渲染?
  • 这个库'com.github.barteksc:android-pdf-viewer:3.1.0-beta.1'

标签: android


【解决方案1】:

使用下面的方法并传递上下文和本地路径作为参数。

public static void openFile(Context context, String localPath) {
    // Create URI
    try {
        File file = new File(localPath);

        Uri uri = Uri.fromFile(file);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        // Check what kind of file you are trying to open, by comparing the url with extensions.
        // When the if condition is matched, plugin sets the correct intent (mime) type,
        // so Android knew what application to use to open the file
        if (file.toString().contains(".pdf")) {
            // PDF file
            intent.setDataAndType(uri, "application/pdf");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }


    } catch (Exception e) {
        e.printStackTrace();
    }
}

【讨论】:

  • 我不想在默认 pdf 阅读器上打开我已经使用 git hub 库创建了一个 pdf 查看器所以当我从下载任务跳转到它关闭的 pdf 查看器时如何在该查看器上打开该 pdf该应用程序,以便我如何在 PDF Viewer 上打开 pdf
【解决方案2】:

在执行后,

使用 Kotlin,

var extension: String? = null

        val file = File(Environment.getExternalStorageDirectory().toString() + "/" + "android" + "/" + "Data" + "/"+ "foldername"+"/"+"Filename")

        val builder = StrictMode.VmPolicy.Builder()
        StrictMode.setVmPolicy(builder.build())
        val uri = Uri.fromFile(file)

        val intent = Intent(Intent.ACTION_VIEW)
        // Check what kind of file you are trying to open, by comparing the url with extensions.
        // When the if condition is matched, plugin sets the correct intent (mime) type,
        // so Android knew what application to use to open the file
         if (uri.toString().contains(".pdf")) {
            // PDF file
            intent.setDataAndType(uri, "application/pdf")
        } 
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        mContext!!.startActivity(intent)

通过使用 Java

 String extension = null;

        File file = new File(Environment.getExternalStorageDirectory().toString() + "/" + "android" + "/" + "Data" + "/" + "foldername" + "/" + "Filename")

        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
        Uri uri = Uri.fromFile(file);

        Intent intent = new Intent(Intent.ACTION_VIEW);
        // Check what kind of file you are trying to open, by comparing the url with extensions.
        // When the if condition is matched, plugin sets the correct intent (mime) type,
        // so Android knew what application to use to open the file
        if (uri.toString().contains(".pdf")) {
            // PDF file
            intent.setDataAndType(uri, "application/pdf");
        }
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       startActivity(intent);

【讨论】:

  • 但我想在 pdf 查看器(Git Hub)库中查看,当我尝试从该活动跳转到该活动时,它现在关闭了应用程序我如何在 PDF 查看器库中查看 pdf ' com.github.barteksc:android-pdf-viewer:3.1.0-beta.1'
  • 它有完整的文档,并且在 github 中有示例应用程序。仔细阅读。
  • 首先你也没有提到你不需要使用默认的pdf查看器打开它
【解决方案3】:

假设您已经将该库集成到您的项目中。将此添加到您要在其中查看 PDF 并将可见性设置为 GONE 的 XML 布局中。

<com.github.barteksc.pdfviewer.PDFView
    android:id="@+id/pdfView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="gone"/>

在postExecute(Void result)方法中,根据here给出的文档设置pdf视图,并将pdfView的可见性设置为View.VISIBLE。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-08
    • 2017-09-02
    • 1970-01-01
    • 2017-10-27
    • 1970-01-01
    • 2018-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多