如何允许用户在android中仅从内部和外部存储器中选择pdf文件 [英] How to allow user to select only pdf file from internal and external memory in android

查看:24
本文介绍了如何允许用户在android中仅从内部和外部存储器中选择pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为我可以打开下载文件夹,但 PDF 看起来像是被禁用了,所以我无法选择 PDF 文件.有没有其他方法可以实现这一目标?

As I am able to open the Downloads folder but PDF looks like disabled so I am not able to select PDF files. Is there any other way to achieve this ?

这是按钮点击的代码

case R.id.pdf_Upload:
               Intent intent = new Intent();
            intent.setType("pdf/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent, "Select Pdf"), REQUESTCODE_PICK_Pdf);

            break; 

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {

    case REQUESTCODE_PICK_Pdf:
                if (requestCode == REQUESTCODE_PICK_Pdf && resultCode == RESULT_OK
                        && null != data) {
                    Uri selectedPdf = data.getData();

                    PdfSelected.setVisibility(View.VISIBLE);
                    if (selectedPdf.getLastPathSegment().endsWith("pdf")) {


                        System.out.println("Uri of selected pdf---->" + selectedPdf.toString());
                    } else if (resultCode == RESULT_CANCELED){
                        Toast.makeText(this, "Invalid file type", Toast.LENGTH_SHORT).show();
                    }
                }
    }

权限

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />

推荐答案

这对我有用

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("application/pdf");
startActivityForResult(intent, SAVE_REQUEST_CODE);

这篇关于如何允许用户在android中仅从内部和外部存储器中选择pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆