How to allow user to select only PDF file from internal and external memory in android

How can I open the Downloads folder but the PDF appears to be disabled so I cannot select the PDF files. Is there any other way to achieve this?

Here is the code for the Click button

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

      

Access rights

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

      

+3


source to share


1 answer


this cod works for me



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

      

+3


source







All Articles