intent.setType(“ application / pdf”)允许任何文件选择

时间:2019-01-02 07:42:24

标签: android android-intent

我正在制作一个应用,希望在其中提供上传pdf文件的选项。我已经编写了这段代码,但是它允许选择任何类型的文档。我只允许选择pdf文件。我正在定位API级别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;

2 个答案:

答案 0 :(得分:2)

这对我有用。

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

也许这会对您有所帮助。

答案 1 :(得分:1)

我遇到了这个问题,唯一适合我的解决方法是检查onActivityResults方法是否数据字符串包含我们不希望的扩展名,例如jpg,jpeg等,我建议您继续选择文件您不希望在loggat中检查其数据字符串,因此我正在使用以下代码

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