SKK
SKK

Reputation: 1719

Android Getting pdf file's path from my app

I am doing an android application. The objective is given below.

Objective:

If the user want to share the pdf (or) image file form anywhere in their android mobile device, they will have the option of my app in the share list. I want to know, whether the user selected the pdf file or an image. For that I used the following code.

In AndroidManifest.xml

<activity
    android:name="com.share.FileActivity"
    android:screenOrientation="portrait"
    android:theme="@android:style/Theme.NoTitleBar" >
    <intent-filter>
        <action android:name="android.intent.action.SEND" />
        <data android:mimeType="application/pdf" />                
        <data android:mimeType="image/*" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

In FileActivity.java, to get the selected file URI

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent myintent = getIntent();
    Bundle extras = myintent.getExtras();
    String action = myintent.getAction();
    if (Intent.ACTION_SEND.equals(action)) {
        if (extras.containsKey(Intent.EXTRA_STREAM)) {
            URI uri = (Uri) extras.getParcelable(Intent.EXTRA_STREAM);
            .
            .
        }
    }
}

Issue:

If I open pdf from the file explorer I am getting the correct URI as "file:///storage/emulated/0/Download/test.pdf". so that I can identify as pdf or image.

But If I open the pdf file from the google drive app, I am getting the following URI "content://com.google.android.apps.docs.storage.legacy/enc%3Du4Ss-AYnclkKv-lfNmA3wY7YBbb_JxRfayDTvfQfHWokHkoy%0A". So I couldn't identify the selected file.

How can I get the file type from the above URI. Help me to fix this issue?

Upvotes: 3

Views: 4404

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006819

How can I get the file type from the above URI

As Selvin notes, call getType() on a ContentResolver(). You can get a ContentResolver by a call to getContentResolver() on any Context, such as your Activity.

Upvotes: 2

Related Questions