简体   繁体   English

在Android上下载并打开PDF文件

[英]download and open PDF file on android

Does somebody know how i could fix this error? 有人知道我该如何解决这个错误? I assume the window leaked has something to do with the progress dialog but i dont know how to fix the problem since i just followed tutorials provided in this 2 links 我认为泄漏窗口进度对话框有关,但是我不知道如何解决该问题,因为我只是按照这2个链接中提供的教程进行操作

first http://www.coderzheaven.com/2012/04/29/download-file-android-device-remote-server-custom-progressbar-showing-progress/ 首先http://www.coderzheaven.com/2012/04/29/download-file-android-device-remote-server-custom-progressbar-showing-progress/

The first link is for downloading the pdf file and showing the progress on progress bar. 第一个链接用于下载pdf文件并在进度条上显示进度。

second http://www.coderzheaven.com/2013/03/06/download-pdf-file-open-android-installed-pdf-reader/ 第二个http://www.coderzheaven.com/2013/03/06/download-pdf-file-open-android-installed-pdf-reader/

The second link is to open the file after the download is completed. 第二个链接是下载完成后打开文件。

Error in logcat logcat中的错误

06-27 11:09:13.592: E/WindowManager(2967): android.view.WindowLeaked: Activity 
com.example.coco.content_co2 has leaked window 
com.android.internal.policy.impl.PhoneWindow$DecorView@430e0198 that was originally  
added here
06-27 11:09:13.592: E/WindowManager(2967):  at   
com.example.coco.content_co2.showProgress(content_co2.java:333)
06-27 11:09:13.592: E/WindowManager(2967):  at   
com.example.coco.content_co2$3.onClick(content_co2.java:103)

Another error in logcat logcat的另一个错误

06-27 11:09:04.572: E/AndroidRuntime(2967): FATAL EXCEPTION: Thread-570
06-27 11:09:04.572: E/AndroidRuntime(2967): java.lang.NullPointerException: file
06-27 11:09:04.572: E/AndroidRuntime(2967):     at 
android.net.Uri.fromFile(Uri.java:441)
06-27 11:09:04.572: E/AndroidRuntime(2967):     at 
com.example.coco.content_co2$5.run(content_co2.java:150)
06-27 11:09:04.572: E/AndroidRuntime(2967):     at  
java.lang.Thread.run(Thread.java:856)
06-27 11:09:13.592: E/WindowManager(2967): android.view.WindowLeaked: Activity  
com.example.coco.content_co2 has leaked window 
com.android.internal.policy.impl.PhoneWindow$DecorView@430e0198 that was originally 
added here
06-27 11:09:13.592: E/WindowManager(2967):  at 
com.example.coco.content_co2.showProgress(content_co2.java:333)
06-27 11:09:13.592: E/WindowManager(2967):  at 
com.example.coco.content_co2$3.onClick(content_co2.java:103)

Here is the part where the error is pointing: 这是错误所指向的部分:

co2_2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            String path = Environment.getExternalStorageDirectory().getPath() + "/CO22.pdf";
            File f = new File(path);
            if (f.exists()) {

                showError("File has been downloaded."); // this is 103
              co2_2.setEnabled(false);

            }
            else {

             showProgress(dwnload_file_path2);

                new Thread(new Runnable() {
                    public void run() {
                        downloadAndOpenPDF2();

                    }
                  }).start();   

        }   
        }});

This if for the progress bar: 如果是进度条:

  void showProgress(String file_path){
                dialog = new Dialog(content_co2.this);
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog.setContentView(R.layout.myprogressdialog);
                dialog.setTitle("Download Progress");

                TextView text = (TextView) dialog.findViewById(R.id.tv1);
                text.setText("Downloading file from ... " + file_path);
                cur_val = (TextView) dialog.findViewById(R.id.cur_pg_tv);
                cur_val.setText("Starting download...");
                dialog.show();     //this is line 333

                pb = (ProgressBar)dialog.findViewById(R.id.progress_bar);
                pb.setProgress(0);

    pb.setProgressDrawable(getResources().getDrawable(R.drawable.green_progress));                 
   }

This is where the null pointer error occurs: 这是发生空指针错误的地方:

  void downloadAndOpenPDF2 () {
        new Thread(new Runnable() {
            public void run() {
                Uri path =  
   Uri.fromFile(downloadFile2(dwnload_file_path2));  //this is line 150
                try {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(path, "application/pdf");
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            finish();
                } catch (ActivityNotFoundException e) {
            showError("PDF Reader application is not installed in your device");

        }
            }
        }).start();

  }

Thanks ... 谢谢 ...

Windowleaked error is due to the progress dialog. 泄漏的错误是由于进度对话框引起的。 Ideally you should be having an AsyncTask and update the progress accordingly. 理想情况下,您应该拥有一个AsyncTask并相应地更新进度。

public class DownloadPDF extends AsyncTask{
Context mContext;
public DownloadPDF(Context context) {
        this.mContext = context;

    }

 @Override
  protected void onPreExecute() {

   progressDialog = new ProgressDialog(mContext);
   progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
   progressDialog.setMessage("Loading...");
   progressDialog.setCancelable(false);
   progressDialog.show();
  }
}

To start DownloadPDF call like 要启动DownloadPDF呼叫,例如

 DownloadPDF task = new DownloadPDF(MyActivity.This);
 task.execute();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM