如何从android中的文件对话框中获取所选文件的扩展名?

时间:2016-03-18 13:25:51

标签: android

我正在开发一个Android应用程序。在我的应用程序中,我使用文件对话框。但我的问题是,我想检索文件对话框所选文件的文件扩展名。请问我该如何实现?

  

这是我的活动的文件对话框关闭回调

@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();
            }
        }
    }

那么请问我该如何才能检索文件扩展名?感谢。

  

这是其中一个答案的编辑部分

这是logcat中的错误。我在CommonHelper中声明了你的功能。 CommonHelper的错误行号是函数的返回行。

  

logcat的

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) 

2 个答案:

答案 0 :(得分:2)

  

在我的应用中,我正在使用文件对话框

不,您似乎在使用ACTION_GET_CONTENTACTION_OPEN_DOCUMENT。 Android没有"文件对话框"。

  

我想检索文件对话框所选文件的文件扩展名

没有必需的文件扩展名。它可能不是文件。

这与网络的工作方式没有显着差异。此网页由此URL标识:

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

此URL也标识:

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

这些都没有文件扩展名,主要是因为没有"文件"这个网页。它是从数据库条目和模板生成的。同样,没有"文件"内容或文件。

在Web和Android中处理此问题的方法是使用MIME类型。对于Uri(示例中为data.getData()),您可以使用ContentResovlergetType()来确定Uri所代表内容的MIME类型。这对应于Web开发中HTTP响应的Content-type标头。

欢迎您尝试使用MimeTypeMap获取MIME类型的建议文件扩展名,但可能没有。您也可以use getLastPathSegment() on the Uri,并查看是否有文件扩展名,但可能没有。

This blog post详细介绍了如何使用Uri中的内容。

答案 1 :(得分:2)

试试这个

如果你的文件有点缀扩展文件,你可以使用这个简单的方法:

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

否则MimeTypeMap应该有所帮助:

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";
    }
}

以下是代码中的外观:

@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();
        }
    }
}

希望这会有所帮助!!