2

I am making an app in which I want to give the option to upload a pdf file. I have written this code but it is allowing to select any type of document to choose. I want to allow only pdf file to select. I am Targeting API level 28.

 case 2:
   Intent pickPdf = new Intent(Intent.ACTION_GET_CONTENT);
   pickPdf.setType("application/pdf");
   pickPdf.addCategory(Intent.CATEGORY_OPENABLE);
   myBundle.putString("type",type);
   try {
     startActivityForResult(Intent.createChooser(pickPdf, "Select a File to Upload"),103);
   } catch (ActivityNotFoundException e) {
     Toast.makeText(GuarantorDocsupload.this, "Please Install a File Manager",Toast.LENGTH_SHORT).show();
   }
   break;
Jaymin
  • 2,879
  • 3
  • 19
  • 35
Ajay
  • 59
  • 1
  • 7

2 Answers2

3

This works for me.

private static final int STORAGE_PERMISSION_CODE = 123;

      Intent intent = new Intent();
      intent.setAction(Intent.ACTION_GET_CONTENT);
      intent.addCategory(Intent.CATEGORY_OPENABLE);
      intent.setType("application/pdf");
      intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
      intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);

try {
     startActivityForResult(Intent.createChooser(intent, "Select Your .pdf File"), PICK_PDF_REQUEST);
   } catch (ActivityNotFoundException e) {
     Toast.makeText(GuarantorDocsupload.this, "Please Install a File Manager",Toast.LENGTH_SHORT).show();
   }

Perhaps this will help you.

Rahul Kushwaha
  • 5,473
  • 3
  • 26
  • 30
  • its working on marshmallow but when i tried on android O its not working. Its allowing all files to select – Ajay Jan 12 '19 at 17:49
1

I faced this problem, and the only wraparound that worked with me is to check in the onActivityResults method if the data string contains the extensions we don't want such as jpg, jpeg, etc, i suggest you keep choosing the files you don't want and check its data string in the loggat, so i am using the following code

if(requestCode == BookDialog.ChooseFile){
        if(data!=null) {

            final Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("application/pdf");
            intent.addCategory(Intent.CATEGORY_OPENABLE);

            String dataUri = data.getDataString();

            if(dataUri!=null&&(dataUri.contains("jpg")||dataUri.contains("png")||dataUri.contains("jpeg")
            ||dataUri.contains("gif")||dataUri.contains("image")||dataUri.contains("video")||dataUri.contains("audio")
            ||dataUri.contains("mp3")||dataUri.contains("mp4"))){
                Toast.makeText(getActivity(),"Please select only a text file\n (pdf or word)",Toast.LENGTH_LONG).show();

                startActivityForResult(Intent.createChooser(intent,"ChooseFile"),BookDialog.ChooseFile);
            }else if(dataUri.contains("apk")){

                new AlertDialog.Builder(getActivity()).setTitle("My apology").setMessage("We may work on this issue in the near future, but can you" +
                        " choose this file from the directory it belongs to? :/").setPositiveButton("Apology accepted", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        startActivityForResult(intent,BookDialog.ChooseFile);

                    }
                }).setNegativeButton("Not accepted", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(getActivity(),"Sorry for this frustration :(",Toast.LENGTH_LONG).show();
                        getDialog().dismiss();
                    }
                }).create().show();
            }else {

                Cursor c = getActivity().getContentResolver().query(data.getData(), null, null, null, null);
                if (c != null) {
                    c.moveToFirst();
                }
                String filename = c.getString(c.getColumnIndex(OpenableColumns.DISPLAY_NAME));
                fileLink.setText(filename);
            }
moumenShobakey
  • 426
  • 5
  • 14