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

前端 未结 7 2059
情话喂你
情话喂你 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:19

    The simple solution is

       Intent gallery=new Intent();
            gallery.setType("application/*");
            gallery.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(gallery,pick);
    

    Note: pick is int variable.

    0 讨论(0)
  • 2020-12-04 22:21
    intent.setType("image/*|application/pdf|audio/*");
    

    Maybe this is what you want.

    0 讨论(0)
  • 2020-12-04 22:36

    It looks like you just want to see if those intents can be resolved. This might be a better approach:

     private void showFileChooser() {
    
        Intent intentPDF = new Intent(Intent.ACTION_GET_CONTENT);
        intentPDF.setType("application/pdf");
        intentPDF.addCategory(Intent.CATEGORY_OPENABLE);
    
        Intent intentTxt = new Intent(Intent.ACTION_GET_CONTENT);
        intentTxt.setType("text/plain");
        intentTxt.addCategory(Intent.CATEGORY_OPENABLE);
    
        Intent intentXls = new Intent(Intent.ACTION_GET_CONTENT);
        intentXls.setType("application/x-excel");
        intentXls.addCategory(Intent.CATEGORY_OPENABLE);
    
        PackageManager packageManager = getPackageManager();
    
        List activitiesPDF = packageManager.queryIntentActivities(intentPDF,
        PackageManager.MATCH_DEFAULT_ONLY);
        boolean isIntentSafePDF = activitiesPDF.size() > 0;
    
        List activitiesTxt = packageManager.queryIntentActivities(intentTxt,
        PackageManager.MATCH_DEFAULT_ONLY);
        boolean isIntentSafeTxt = activitiesTxt.size() > 0;
    
        List activitiesXls = packageManager.queryIntentActivities(intentXls,
        PackageManager.MATCH_DEFAULT_ONLY);
        boolean isIntentSafeXls = activitiesXls.size() > 0;
    
        if (!isIntentSafePDF || !isIntentSafeTxt || !isIntentSafeXls){
    
            // Potentially direct the user to the Market with a Dialog
    
        }
    
    }
    

    References:

    http://developer.android.com/training/basics/intents/sending.html

    How do I determine if Android can handle PDF

    0 讨论(0)
  • 2020-12-04 22:39

    You should to check this link http://www.androidsnippets.com/open-any-type-of-file-with-default-intent.html. Also read through Mime Type.

    This is how I have implemented for any file or selected mime type file

    String[] mimeTypes =
    {"image/*","application/pdf","application/msword","application/vnd.ms-powerpoint","application/vnd.ms-excel","text/plain"};
    
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        intent.setType(mimeTypes.length == 1 ? mimeTypes[0] : "*/*");
        if (mimeTypes.length > 0) {
           intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
        }
    } else {
        String mimeTypesStr = "";
        for (String mimeType : mimeTypes) {
            mimeTypesStr += mimeType + "|";
        }
        intent.setType(mimeTypesStr.substring(0,mimeTypesStr.length() - 1));
    }
    startActivityForResult(Intent.createChooser(intent,"ChooseFile"), 0);
    
    0 讨论(0)
  • 2020-12-04 22:39

    You can use Intent.ACTION_OPEN_DOCUMENT,

    Each document is represented as a content:// URI backed by a DocumentsProvider, which can be opened as a stream with openFileDescriptor(Uri, String), or queried for DocumentsContract.Document metadata.

    All selected documents are returned to the calling application with persistable read and write permission grants. If you want to maintain access to the documents across device reboots, you need to explicitly take the persistable permissions using takePersistableUriPermission(Uri, int).

    Callers must indicate the acceptable document MIME types through setType(String). For example, to select photos, use image/*. If multiple disjoint MIME types are acceptable, define them in EXTRA_MIME_TYPES and setType(String) to */*.

    For the more details, please refer this link

    http://developer.android.com/reference/android/content/Intent.html#ACTION_OPEN_DOCUMENT

    Note that this is only available on API Level 19+.

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题