Android intent.setType(“application/pdf”)允许选择任何文件
我正在制作一个应用程序,我想在其中提供上传pdf文件的选项。我已经编写了这段代码,但它允许选择任何类型的文档。我只允许选择pdf文件。我的目标是API级别28Android intent.setType(“application/pdf”)允许选择任何文件,android,android-intent,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.p
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;
这对我有用
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();
}
也许这会对您有所帮助。我遇到了这个问题,我唯一能做的概括就是签入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);
}
你可以从这里得到帮助。首先阅读问题。我不想改变你的看法。我想给select选项@RAHULits正在研制棉花糖,但当我在安卓系统上试用时,它就不起作用了。它允许选择所有文件