Wai Yan Hein
Wai Yan Hein

Reputation: 14791

How to get the chosen file's extension from file dialog in Android?

I am developing an Android app. In my app I am using file dialog. But what my problem is, I want to retrieve the file extension of chosen file of file dialog.

This is the file dialog close call back of My Activity

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == RESULT_OK && data != null && data.getData() != null) {
            Uri filePath = data.getData();
            try {
                //Getting the Bitmap from Gallery
                bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
                //Setting the Bitmap to ImageView
                ivPreviewImage.setImageBitmap(bitmap);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

How can I retrieve the file extension only?

Update

This is the error in logcat. I declared your function in CommonHelper. The error line number of CommonHelper is the return line of your function.

03-18 13:47:09.812 815-815/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: FATAL EXCEPTION: main
03-18 13:47:09.812 815-815/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/73 }} to activity {com.blog.waiyanhein.mmfashion.mmfashion/com.blog.waiyanhein.mmfashion.mmfashion.CreateItemActivity}: java.lang.StringIndexOutOfBoundsException: length=25; index=-1
03-18 13:47:09.812 815-815/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime:     at android.app.ActivityThread.deliverResults(ActivityThread.java:3319)
03-18 13:47:09.812 815-815/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime:     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3362)
03-18 13:47:09.812 815-815/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime:     at android.app.ActivityThread.access$1100(ActivityThread.java:141)
03-18 13:47:09.812 815-815/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
03-18 13:47:09.812 815-815/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:99)
03-18 13:47:09.812 815-815/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:137)
03-18 13:47:09.812 815-815/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5041)
03-18 13:47:09.812 815-815/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
03-18 13:47:09.812 815-815/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:511)
03-18 13:47:09.812 815-815/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-18 13:47:09.812 815-815/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-18 13:47:09.812 815-815/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
03-18 13:47:09.812 815-815/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime:  Caused by: java.lang.StringIndexOutOfBoundsException: length=25; index=-1
03-18 13:47:09.812 815-815/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime:     at java.lang.String.indexAndLength(String.java:579)
03-18 13:47:09.812 815-815/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime:     at java.lang.String.substring(String.java:1438)
03-18 13:47:09.812 815-815/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime:     at com.blog.waiyanhein.mmfashion.model.CommonHelper.getExtensionFromUri(CommonHelper.java:126)
03-18 13:47:09.812 815-815/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime:     at com.blog.waiyanhein.mmfashion.mmfashion.CreateItemActivity.onActivityResult(CreateItemActivity.java:288)
03-18 13:47:09.812 815-815/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime:     at android.app.Activity.dispatchActivityResult(Activity.java:5293)
03-18 13:47:09.812 815-815/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime:     at android.app.ActivityThread.deliverResults(ActivityThread.java:3315)
03-18 13:47:09.812 815-815/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime:     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3362) 
03-18 13:47:09.812 815-815/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime:     at android.app.ActivityThread.access$1100(ActivityThread.java:141) 
03-18 13:47:09.812 815-815/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282) 
03-18 13:47:09.812 815-815/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:99) 
03-18 13:47:09.812 815-815/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:137) 
03-18 13:47:09.812 815-815/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5041) 
03-18 13:47:09.812 815-815/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
03-18 13:47:09.812 815-815/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:511) 
03-18 13:47:09.812 815-815/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
03-18 13:47:09.812 815-815/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
03-18 13:47:09.812 815-815/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method) 

Upvotes: 1

Views: 166

Answers (2)

Gueorgui Obregon
Gueorgui Obregon

Reputation: 5087

Try this

If your file have doted extension file, you could use this simple method:

public String getExtensionFromUri(Uri fileUri) {
    String filePath = fileUri.getPath();
    try {
        return filePath.substring(filePath.lastIndexOf(".") + 1);
    }catch(Exception e){
        return "no extension";
    }
}

Otherwise MimeTypeMap should help:

public String getMIMETypeFromUri(Uri fileUri) {
    try {
        ContentResolver cR = context.getContentResolver();
        MimeTypeMap mime = MimeTypeMap.getSingleton();
        return  mime.getExtensionFromMimeType(cR.getType(fileUri));
    }catch(Exception e){
        return "no mime found";
    }
}

Here is how its looks like in your code:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1 && resultCode == RESULT_OK && data != null && data.getData() != null) {
        Uri filePath = data.getData();
        Log.d(TAG,"Extension from selected file:" + getExtensionFromUri(filePath));   
        Log.d(TAG,"Extension from MIME:" + getMIMETypeFromUri(filePath));       
         try {
            //Getting the Bitmap from Gallery
            bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
            //Setting the Bitmap to ImageView
            ivPreviewImage.setImageBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Hope this helps!!

Upvotes: 2

CommonsWare
CommonsWare

Reputation: 1006809

In my app I am using file dialog

No, you appear to be using ACTION_GET_CONTENT or possibly ACTION_OPEN_DOCUMENT. Android does not have a "file dialog".

I want to retrieve the file extension of chosen file of file dialog

There is no required file extension. It may not be a file.

This is not significantly different than how the Web works. This Web page is identified by this URL:

https://stackoverflow.com/questions/36085617/how-to-get-the-chosen-files-extension-from-file-dialog-in-android

It is also identified by this URL:

https://stackoverflow.com/q/36085617/115145

Neither of those have a file extension, largely because there is no "file" for this Web page. It is generated from database entries and templates. Similarly, there is no "file" for content or documents.

The way you handle this in the Web, and in Android, is to use the MIME type. For a Uri (data.getData() in your example), you can use ContentResovler and getType() to determine the MIME type of the content represented by the Uri. This corresponds to the Content-type header of an HTTP response in Web development.

You are welcome to attempt to use MimeTypeMap to get a suggested file extension for the MIME type, though there may not be one. You are also welcome to use getLastPathSegment() on the Uri, and see if there is a file extension on it, though there may not be one.

This blog post covers more about how to consume content from a Uri.

Upvotes: 2

Related Questions