简体   繁体   English

使用 Android 存储访问框架获取文件扩展

[英]Get FIle extension using Android Storage Access Framework

I am using the Storage Access Framework in android for selecting images and uploading in my app.我正在使用 android 中的存储访问框架来选择图像并在我的应用程序中上传。 Before uploading these images to my server, i also process these images (resizing).在将这些图像上传到我的服务器之前,我还会处理这些图像(调整大小)。 However, in the URI returned by the SAF, there is no extension appended, i don't know how to get extension of the file.但是,在 SAF 返回的 URI 中,没有附加扩展名,我不知道如何获取文件的扩展名。 I need to differentiate between png and jpeg.我需要区分 png 和 jpeg。 How can i achieve that?我怎样才能做到这一点? Example uri returned from SAF - content://com.android.providers.media.documents/document/image%3A245309从 SAF 返回的示例 uri - content://com.android.providers.media.documents/document/image%3A245309

My code is something like this我的代码是这样的


if (resultData != null) {
                        final ClipData clipData = resultData.getClipData();
                        int takeFlags = resultData.getFlags();
                        takeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                        ArrayList<Uri> uris = new ArrayList<Uri>();
                        if(clipData != null){
                            for (int i = 0; i < clipData.getItemCount(); i++) {
                                ClipData.Item item = clipData.getItemAt(i);
                                Uri dataUri = item.getUri();
                                if (dataUri != null) {
                                    uris.add(dataUri);
                                    Log.d(TAG, "File URI : " + dataUri.toString());
                                }
                            }
                        }else{
                            Uri dataUri = resultData.getData();
                            if (dataUri != null) {
                                uris.add(dataUri);
                            }
                        }
                        processFiles(uris);
                }

I need to differentiate between png and jpeg.我需要区分 png 和 jpeg。 How can i achieve that?我怎样才能做到这一点?

Call getType() on a ContentResolver , passing in the Uri . ContentResolver上调用getType() ,传入Uri This will return the MIME type associated with the content, such as image/png .这将返回与内容关联的 MIME 类型,例如image/png

Using Simple Storage is much easier:使用简单存储要容易得多:

MediaFile file = new MediaFile(context, uri);
String extension = file.getExtension();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Android中使用Storage Access Framework保存文件 - Saving file using Storage Access Framework in Android 一起使用Android Storage Access Framework和直接文件访问 - Using Android Storage Access Framework and direct file access together 如何使用Android Storage Access Framework获取DocumentContract - How to get a DocumentContract using Android Storage Access Framework 如何使用android存储访问框架附加到文件? - How to append to a file using android Storage access framework? Android - 使用存储访问框架加速文件删除 - Android - speed up file deletion using Storage Access Framework 如何使用 android 存储访问框架正确覆盖文件内容 - How to properly overwrite content of file using android storage access framework 如何使用存储访问框架访问“/storage/emulated/0/Android/media/”? - How to get access to "/storage/emulated/0/Android/media/" using Storage Access Framework? Android Storage Access FrameWork具有自定义文件系统 - Android Storage Access FrameWork with custom file system Android - 存储访问框架 - Uri 到本地文件 - Android - Storage Access Framework - Uri into local file 扩展名为.gpx的文件无法通过Storage Access Framework加载 - The file with the .gpx extension can not be loaded via Storage Access Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM