how to make intent.setType for pdf,xlsx and txt file android?

前端 未结 7 2078
情话喂你
情话喂你 2020-12-04 21:48

I want to choose only pdf, xlsx and txt file from storage but intent.setType can do only one file(eg.txt file only (or) pdf file only). Is it possible to get all three files

7条回答
  •  时光取名叫无心
    2020-12-04 22:39

    You should use the system function to find the specific mimeType of the file that you are trying to access(Like the "application/pdf"). If you are dealing with any unknown type, it will probably be hard to find the type. Hope this code will save you !

    String url=Environment.getExternalStorageDirectory().getAbsolutePath()+"/Saves/contacts.vcf";
    File file = new File(url);
    Intent intent = new Intent(Intent.ACTION_VIEW);
    String mimeType=MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(url));
    intent.setDataAndType(Uri.fromFile(file), mimeType);
    Intent intent1 = Intent.createChooser(intent, "Open With");
    startActivity(intent1);
    

    Source HERE

提交回复
热议问题