作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个 Android 应用程序。在我的应用程序中,我正在使用文件对话框。但我的问题是,我想检索文件对话框中所选文件的文件扩展名。
这是我的 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();
}
}
}
如何只获取文件扩展名?
这是 logcat 中的错误。我在 CommonHelper 中声明了你的函数。 CommonHelper 的错误行号是你函数的返回行。
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)
最佳答案
试试这个
如果你的文件有带扩展名的文件,你可以使用这个简单的方法:
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();
}
}
}
希望这对您有所帮助!
关于android - 如何从 Android 的文件对话框中获取所选文件的扩展名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36085617/
我是一名优秀的程序员,十分优秀!