gpt4 book ai didi

java - Pdf 文件无法在 Android 中打开

转载 作者:行者123 更新时间:2023-12-01 09:18:11 24 4
gpt4 key购买 nike

我想在点击“pdfButton”时打开一个 pdf 文件(可在 Android 手机的“下载”文件夹中找到)执行操作时,没有任何反应,没有记录错误或显示 pdf 文件。有人可以帮忙吗?

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

}



}

最佳答案

如果您使用调试器单步执行代码,或者放入更多日志记录语句,我怀疑您会发现 file.exists() 返回 false。而且,目前,在这种情况下您无需执行任何操作。

I would like to open a pdf file ( which is available in Downloads folder in Android phone)

那不是您的代码所寻找的地方。替换:

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

与:

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

另请注意,您的 file.exists() 调用意味着您需要持有 READ_EXTERNAL_STORAGE 权限。

关于java - Pdf 文件无法在 Android 中打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40370057/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com