Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何允许用户仅从android的内部和外部内存中选择pdf文件_Android_Pdf_Android Studio - Fatal编程技术网

如何允许用户仅从android的内部和外部内存中选择pdf文件

如何允许用户仅从android的内部和外部内存中选择pdf文件,android,pdf,android-studio,Android,Pdf,Android Studio,由于我可以打开下载文件夹,但PDF看起来已禁用,因此我无法选择PDF文件。有没有其他方法可以做到这一点 这是单击按钮的代码 case R.id.pdf_Upload: Intent intent = new Intent(); intent.setType("pdf/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityFo

由于我可以打开下载文件夹,但PDF看起来已禁用,因此我无法选择PDF文件。有没有其他方法可以做到这一点

这是单击按钮的代码

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" />

这个cod对我有用

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

为了补充Omar给出的答案,您仍然可以添加此选项,以限制用户仅使用此简单行选择内部和外部文档

intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);

检查此链接:您是否安装了pdf阅读器?@Naveentamakar我想打开存储,其中只有pdf文件,用户可以选择pdf文件,然后我可以上传它。在“我的代码下载”文件夹中-->打开-->Pdf文件-->未选择,因为disabled@NaveenTamrakar在讨论任何事情和评论你的观点之前,仔细阅读问题opinions@quicklearner. 在讨论任何事情和评论你无用的观点之前,仔细阅读问题