Search code examples
androidandroid-intentopen-withbrowsable

Android - get file path of a selected pdf, doc, ppt or xls in my app


I am developing an android application.

My AIM:

If the user open the pdf, doc, ppt or xls files in any folder inside the SDCard (or) dropbox (or) email etc..., they must navigate into my app(In my app, I will do some process based on the files they selected).

What I did:

If the user click on the PDF file, I can get the filepath and they are navigate into my app. For this I have done the following code.

Code snippet:

In the AndroidManifest.xml, I have added the following code:

        <activity android:name="com.openwith.OpenwithActivity" >
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <data android:scheme="file" />
                <data android:mimeType="*/*" />
                <data android:scheme="http" android:host="*" android:pathPattern=".*\\.pdf" />
                <data android:scheme="https" android:host="*" android:pathPattern=".*\\.pdf" />
                <data android:scheme="content" android:host="*" android:pathPattern=".*\\.pdf" />
                <data android:scheme="file" android:host="*" android:pathPattern=".*\\.pdf" />
            </intent-filter>
        </activity>

In OpenwithActivity.java

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.WebView;

public class OpenwithActivity extends Activity {

        @SuppressLint("SetJavaScriptEnabled")
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_openwith);

            Intent intent = getIntent();
            Uri data = intent.getData();
            if (data != null) {
                  String filePath = data.getPath();
            } 
        }

}

What I Need:

I am getting only the pdf file's path. If I want get doc, ppt and xls file's path, what I need to add in the AndroidManifest.xml?

I have searched in the internet. But I didn't get any clear documents (or) tutorials. Can anyone hep me to do this?


Solution

  • Add the same intent-filter entries for the other file types as you did for the pdf.

    <data android:scheme="http" android:host="*" android:pathPattern=".*\\.ppt" />
    <data android:scheme="https" android:host="*" android:pathPattern=".*\\.ppt" />
    <data android:scheme="content" android:host="*" android:pathPattern=".*\\.ppt" />
    <data android:scheme="file" android:host="*" android:pathPattern=".*\\.ppt" />
    <data android:scheme="http" android:host="*" android:pathPattern=".*\\.xls" />
    <data android:scheme="https" android:host="*" android:pathPattern=".*\\.xls" />
    <data android:scheme="content" android:host="*" android:pathPattern=".*\\.xls" />
    <data android:scheme="file" android:host="*" android:pathPattern=".*\\.xls" />
    <data android:scheme="http" android:host="*" android:pathPattern=".*\\.doc" />
    <data android:scheme="https" android:host="*" android:pathPattern=".*\\.doc" />
    <data android:scheme="content" android:host="*" android:pathPattern=".*\\.doc" />
    <data android:scheme="file" android:host="*" android:pathPattern=".*\\.doc" />
    

    And for the images, this should do:

    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
    
        <data android:mimeType="image/*" android:scheme="http" android:host="*" />
        <data android:mimeType="image/*" android:scheme="https" android:host="*" />
        <data android:mimeType="image/*" android:scheme="content" android:host="*" />
        <data android:mimeType="image/*" android:scheme="file" android:host="*" />
    </intent-filter>