需要在android中仅上传pdf或任何doc类型格式

时间:2016-11-24 05:08:30

标签: java android

我尝试仅上传pdf或任何文档类型文件,但也添加了.gif和.psd格式。我只需要上传支持的文件格式。

resume_add.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (ActivityCompat.checkSelfPermission(MyProfileActivity.this, android.Manifest
                        .permission.WRITE_EXTERNAL_STORAGE) != PackageManager
                        .PERMISSION_GRANTED) {
                    requestPermissions(new String[]{android.Manifest.permission
                            .WRITE_EXTERNAL_STORAGE}, 1);
                } else {
                    Intent target = FileUtils.createGetContentIntent();
                    Intent intent = Intent.createChooser(target, "Select File");
                    intent.setType("application/msword,application/pdf");
                    try {
                        startActivityForResult(intent, SELECT_PICTURE);
                    } catch (ActivityNotFoundException ignored) {

                    }
                }
            } else {
                Intent target = FileUtils.createGetContentIntent();
                Intent intent = Intent.createChooser(target, "Select File");
                try {
                    startActivityForResult(intent, SELECT_PICTURE);
                } catch (ActivityNotFoundException ignored) {

                }
            }

        }
    });

1 个答案:

答案 0 :(得分:0)

使用以下代码段

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

它为我工作,希望它对你有用!