简体   繁体   English

pdf文件无法在Android中打开

[英]Pdf file is not opening in Android

I would like to open a pdf file ( which is available in Downloads folder in Android phone) during on click on the 'pdfButton' While performing the action, nothing happens, there are either no errors logged or pdf file is displayed. 我想在单击“ pdfButton”时打开一个pdf文件(可在Android手机的“下载”文件夹中找到)。执行该操作时,什么也没有发生,没有记录错误或显示了pdf文件。 Could some one please help ? 有人可以帮忙吗?

package com.mycompany.myfirstglapp;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceView;
import android.webkit.WebView;
import android.widget.Toast;
import java.io.File;

/**
 * Created by admin on 1/11/2016.
 */

public class PdfActivity extends Activity {
    private SurfaceView surface;
    Button pdfButton;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pdf);
        surface = (SurfaceView) findViewById(R.id.pdfSurface);
        pdfButton = (Button) findViewById(R.id.pdfView);

        pdfButton .setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    // On click will call the showPdf method to display the pdf file in sd card or downloads 

                    showPdf(view);
                }
            });



    }


   public void showPdf(View view)  {

        // The pdf file [LawsofthegamewebEN_Neutral.pdf] is avaialble in Android > Downloads folder.

        File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+ "/LawsofthegamewebEN_Neutral.pdf");

        if (file.exists()) {
            Uri path = Uri.fromFile(file);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(path, "application/pdf");
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            try {
                startActivity(intent);
            }
            catch (ActivityNotFoundException e) {
                Toast.makeText(PdfActivity.this,
                        "No Application Available to View PDF",
                        Toast.LENGTH_SHORT).show();
            }
        }

    }



}

If you step through the code with your debugger, or put more logging statements in, I suspect that you will find that file.exists() returns false . 如果您使用调试器逐步执行代码,或放入更多日志记录语句,我怀疑您会发现file.exists()返回false And, at the moment, you do not do anything in that case. 而且,在这种情况下,您现在什么也不做。

I would like to open a pdf file ( which is available in Downloads folder in Android phone) 我想打开一个pdf文件(可在Android手机的“下载”文件夹中找到)

That is not where your code is looking. 那不是您的代码正在寻找的地方。 Replace: 更换:

File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+ "/LawsofthegamewebEN_Neutral.pdf");

with: 与:

File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "LawsofthegamewebEN_Neutral.pdf");

Also note that your file.exists() call means that you will need to hold the READ_EXTERNAL_STORAGE permission. 还要注意,您的file.exists()调用意味着您将需要拥有READ_EXTERNAL_STORAGE权限。

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

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