首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用AndroidPdfViewer库显示存储中的pdf文件?

如何使用AndroidPdfViewer库显示存储中的pdf文件?
EN

Stack Overflow用户
提问于 2020-12-22 21:09:10
回答 4查看 555关注 0票数 1

我想使用AndroidPdfViewer从外部存储(下载/Pdfs/myfile.pdf)加载一个pdf文件,但它显示空白屏幕,没有任何错误。我尝试了很多方法,但都不起作用。

代码语言:javascript
复制
public class PdfViewActivity2 extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        File path = new File(Environment.getExternalStorageDirectory().getPath() + "/Download/Pdfs/myfile.pdf");
        PDFView pdfView = findViewById(R.id.pdfView);
        pdfView.fromFile(path).load();

我在我的"Download/Pdfs/myfile.pdf“中有一个pdf文件,我使用上面的代码加载了这个文件,但它不起作用。我已经从设置中手动授予了存储权限。有人能纠正我哪里出了错吗?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2020-12-30 02:29:42

我已经测试了你的代码,它在Android 10设备上运行良好。您缺少以下内容:

1.在Android Manifest文件中添加READ_EXTERNAL_STORAGE权限

代码语言:javascript
复制
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

在应用程序标签中,将requestLegacyExternalStorage添加到true,以便能够访问Android10及更高版本设备上的外部存储。

代码语言:javascript
复制
<application
        android:requestLegacyExternalStorage="true"

2.验证设备上"/Download/Pdfs/myfile.pdf“路径下是否存在pdf。

3.使用以下代码更改您的活动,首先在onCreate方法中在运行时请求外部存储权限:

代码语言:javascript
复制
public class PdfViewActivity2 extends AppCompatActivity {

    private static final int READ_STORAGE_PERMISSION_REQUEST_CODE = 1000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //check if Read External Storage permission was granded
        boolean granded = checkPermissionForReadExtertalStorage();
        if(!granded){
            requestPermissionForReadExtertalStorage();
        }
        else {
           readPdf();
        }
    }

    public boolean checkPermissionForReadExtertalStorage() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            int result = checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE);
            return result == PackageManager.PERMISSION_GRANTED;
        }
        return false;
    }

    public void requestPermissionForReadExtertalStorage() {
        try {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, READ_STORAGE_PERMISSION_REQUEST_CODE);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case READ_STORAGE_PERMISSION_REQUEST_CODE: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // permission was granted. Read Pdf from External Storage
                    readPdf();
                } else {
                    // permission denied. Disable the functionality that depends on this permission.
                }
            }
        }
    }

    private void readPdf(){
        File path = new File(Environment.getExternalStorageDirectory().getPath() + "/Download/Pdfs/myfile.pdf");
        PDFView pdfView = findViewById(R.id.pdfView);
        pdfView.fromFile(path).load();
    }
}
票数 0
EN

Stack Overflow用户

发布于 2020-12-25 23:38:03

首先,将库添加到build.gradle文件中

代码语言:javascript
复制
implementation 'com.github.barteksc:android-pdf-viewer:2.8.2'

要从存储中打开PDF文件,请使用以下代码。有一些注释解释了它的用途。

代码语言:javascript
复制
public class PdfViewActivity2  extends AppCompatActivity {

    // Declare PDFView variable
    private PDFView pdfView;
    private final int PDF_SELECTION_CODE = 99;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Initialize it
        pdfView = findViewById(R.id.pdfView);

        // Select PDF from storage
        // This code can be used in a button
        Toast.makeText(this, "selectPDF", Toast.LENGTH_LONG).show();
        Intent browseStorage = new Intent(Intent.ACTION_GET_CONTENT);
        browseStorage.setType("application/pdf");
        browseStorage.addCategory(Intent.CATEGORY_OPENABLE);
        startActivityForResult(Intent.createChooser(browseStorage, "Select PDF"), PDF_SELECTION_CODE);
    }


    // Get the Uniform Resource Identifier (Uri) of your data, and receive it as a result.
    // Then, use URI as the pdf source and pass it as a parameter inside this method fromUri(Uri uri)
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PDF_SELECTION_CODE && resultCode == Activity.RESULT_OK && data != null) {
            Uri selectedPdfFromStorage = data.getData();
            pdfView.fromUri(selectedPdfFromStorage).defaultPage(0).load();
        }
    }
}
票数 0
EN

Stack Overflow用户

发布于 2020-12-29 21:41:38

在Android 10设备中,您的应用程序无法访问外部存储。

除非您添加

代码语言:javascript
复制
android:requestLegacyExternalStorage="true"

在清单文件的应用程序标记中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65409328

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档