Android intent filter: associate app with file extension

后端 未结 16 1691
北海茫月
北海茫月 2020-11-22 11:06

I have a custom file type/extension that I want to associate my app with.

As far as I know, the data element is made for this purpose, but I can\'t get it working. h

相关标签:
16条回答
  • 2020-11-22 11:47

    For Gmail attachment, you can use:

    <intent-filter android:label="@string/app_name">
      <action android:name="android.intent.action.VIEW" />
      <category android:name="android.intent.category.DEFAULT" />
      <data android:scheme="content" />
      <data android:mimeType="application/pdf" /> <!-- .pdf -->
      <data android:mimeType="application/msword" /> <!-- .doc / .dot -->
      <data android:mimeType="application/vnd.openxmlformats-officedocument.wordprocessingml.document" /> <!-- .docx -->
      <data android:mimeType="application/vnd.ms-excel" />  <!-- .xls / .xlt / .xla -->
      <data android:mimeType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />  <!-- .xlsx -->
      <data android:mimeType="application/vnd.ms-powerpoint" />  <!-- .ppt / .pps / .pot / .ppa -->
      <data android:mimeType="application/vnd.openxmlformats-officedocument.presentationml.presentation" /> <!-- .pptx -->
      <data android:mimeType="application/vnd.openxmlformats-officedocument.presentationml.slideshow" /> <!-- .ppsx -->
      <data android:mimeType="application/zip" /> <!-- .zip -->
      <data android:mimeType="image/jpeg" /> <!-- .jpeg -->
      <data android:mimeType="image/png" /> <!-- .png -->
      <data android:mimeType="image/gif" /> <!-- .gif -->
      <data android:mimeType="text/plain" /> <!-- .txt / .text / .log / .c / .c++ / ... -->
    

    Add as many mime types as needed. I only need those for my project.

    0 讨论(0)
  • 2020-11-22 11:48

    I've been trying to get this to work for ages and have tried basicly all the suggested solutions and still cannot get Android to recognise specific file extensions. I have an intent-filter with a "*/*" mimetype which is the only thing that seems to work and file-browsers now list my app as an option for opening files, however my app is now shown as an option for opening ANY KIND of file even though I've specified specific file extensions using the pathPattern tag. This goes so far that even when I try to view/edit a contact in my contacts list Android asks me if I want to use my app to view the contact, and that is just one of many situations where this occurs, VERY VERY annoying.

    Eventually I found this google groups post with a similar question to which an actual Android framework engineer replied. She explains that Android simply does not know anything about file-extensions, only MIME-types (https://groups.google.com/forum/#!topic/android-developers/a7qsSl3vQq0).

    So from what I've seen, tried and read, Android simply cannot distinguish between file-extensions and the pathPattern tag is basicly a gigantic waste of time and energy. If you are fortunate enough to only need files of a certain mime-type (say text, video or audio), you can use an intent-filter with a mime-type. If you need a specific file-extension or a mime-type not known by Android however then you're out of luck.

    If I'm wrong about any of this please tell me, so far I've read every post and tried every proposed solution I could find but none have worked.

    I could write another page or two about how common these kinds of things seem to be in Android and how screwed up the developer experience is, but I'll save you my angry rantings ;). Hope I saved someone some trouble.

    0 讨论(0)
  • 2020-11-22 11:48

    Those who having issue with other File Manager\Explorer apps, as @yuku and @phyrum-tea answered

    This works with LG default file manager app

         <intent-filter android:label="@string/app_name_decrypt">
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="file" />
                <data android:pathPattern=".*\\.lock" />
                <data android:pathPattern=".*\\..*\\.lock" />
                <data android:pathPattern=".*\\..*\\..*\\.lock" />
            </intent-filter>
    

    but could not work with ES File Explorer and other File Managers so i added

     android:mimeType="*/*"
    

    then it works with ES Explorer but LG file manager could not detect file type so my solution is

         <intent-filter android:label="@string/app_name_decrypt">
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="file" />
                <data android:pathPattern=".*\\.lock" />
                <data android:pathPattern=".*\\..*\\.lock" />
                <data android:pathPattern=".*\\..*\\..*\\.lock" />
            </intent-filter>
            <intent-filter android:label="@string/app_name_decrypt">
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="file"/>
                <data android:scheme="content" />
                <data android:mimeType="*/*" />
                <data android:pathPattern=".*\\.lock" />
                <data android:pathPattern=".*\\..*\\.lock" />
                <data android:pathPattern=".*\\..*\\..*\\.lock" />
            </intent-filter>
    
    0 讨论(0)
  • 2020-11-22 11:48

    // I have tried this code. And it is working well. You can use this code to accept pdf file.

    <intent-filter>
       <action android:name="android.intent.action.SEND" />
       <category android:name="android.intent.category.DEFAULT" />
       <data android:mimeType="application/pdf" />
       <data android:pathPattern=".*\\.pdf" />
       <data android:pathPattern=".*\\..*\\.pdf" />
       <data android:pathPattern=".*\\..*\\..*\\.pdf" />
       <data android:pathPattern=".*\\..*\\..*\\..*\\.pdf" />
    </intent-filter>
    
    0 讨论(0)
  • 2020-11-22 11:49

    You try this it will help for you.Instead of pdf you can use other extensions also. First you have to add read external storage permission in androidmanifest.xml file.

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    

    Then in the androidmanifest file in the Activity tag, you add an intent-filter as shown below.

                <action android:name="android.intent.action.SEND" />
    
                <action android:name="android.intent.action.VIEW" />
    
                 <category android:name="android.intent.category.DEFAULT" />
    
                <data android:mimeType= "application/pdf" />
    
                <data android:host="*" />
    
            </intent-filter>
    

    Finally in your code, you get path of the pdf file as shown below:

    Intent intent=getIntent();
    
    if(intent!=null) {          
    
            String action=intent.getAction();
    
            String type=intent.getType();
    
            if(Intent.ACTION_VIEW.equals(action) && type.endsWith("pdf")) {
    
                // Get the file from the intent object
    
                Uri file_uri=intent.getData();
    
                if(file_uri!=null)
    
                    filepath=file_uri.getPath();
    
                else
    
                    filepath="No file";
    
            }
    
            else if(Intent.ACTION_SEND.equals(action) && type.endsWith("pdf")){
    
                Uri uri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
    
                filepath = uri.getPath();
    
            }
    
    0 讨论(0)
  • 2020-11-22 11:51

    Content URI ftw, and with the intent filter in the manifest... if your files have a custom extension .xyz, add a matching mime type:

            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
    
                <category android:name="android.intent.category.DEFAULT" />
    
                <data
                    android:host="*"
                    android:mimeType="application/xyz"
                    android:scheme="content" />
            </intent-filter>
    

    Some apps such as email seem to convert the extension into a mime type. Now I can click on the attachment in email and have it open in my app.

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