(UriType uri, Context context)
| 2975 | } |
| 2976 | |
| 2977 | @NonNull |
| 2978 | static UriInfo getInfo(UriType uri, Context context) { |
| 2979 | UriInfo result = new UriInfo(); |
| 2980 | |
| 2981 | // https://stackoverflow.com/questions/76094229/android-13-photo-video-picker-file-name-from-the-uri-is-garbage |
| 2982 | DocumentFile dfile = null; |
| 2983 | try { |
| 2984 | dfile = DocumentFile.fromSingleUri(context, uri.getUri()); |
| 2985 | if (dfile != null) { |
| 2986 | result.name = dfile.getName(); |
| 2987 | result.type = dfile.getType(); |
| 2988 | result.size = dfile.length(); |
| 2989 | EntityLog.log(context, "UriInfo dfile " + result + " uri=" + uri); |
| 2990 | } |
| 2991 | } catch (Throwable ex) { |
| 2992 | Log.e(ex); |
| 2993 | } |
| 2994 | |
| 2995 | // Check name |
| 2996 | if (TextUtils.isEmpty(result.name)) |
| 2997 | result.name = uri.getUri().getLastPathSegment(); |
| 2998 | |
| 2999 | // Check type |
| 3000 | if (TextUtils.isEmpty(result.type) && uri.getType() != null) |
| 3001 | result.type = uri.getType(); // Shared type when no document type |
| 3002 | if (!TextUtils.isEmpty(result.type)) |
| 3003 | try { |
| 3004 | new ContentType(result.type); |
| 3005 | } catch (ParseException ex) { |
| 3006 | Log.w(new Throwable(result.type, ex)); |
| 3007 | result.type = null; |
| 3008 | } |
| 3009 | |
| 3010 | if (TextUtils.isEmpty(result.type) || |
| 3011 | "*/*".equals(result.type) || |
| 3012 | "application/*".equals(result.type) || |
| 3013 | "application/octet-stream".equals(result.type)) |
| 3014 | result.type = Helper.guessMimeType(result.name); |
| 3015 | |
| 3016 | if (result.type != null && result.type.startsWith("multipart/")) |
| 3017 | result.type = "application/octet-stream"; |
| 3018 | |
| 3019 | String ext = getExtension(result.name); |
| 3020 | if ("mht".equalsIgnoreCase(ext) || "mhtml".equalsIgnoreCase(ext)) |
| 3021 | result.type = "application/x-mimearchive"; |
| 3022 | |
| 3023 | if (result.size != null && result.size <= 0) |
| 3024 | result.size = null; |
| 3025 | |
| 3026 | EntityLog.log(context, "UriInfo result " + result + " uri=" + uri); |
| 3027 | |
| 3028 | return result; |
| 3029 | } |
| 3030 | |
| 3031 | static class UriInfo { |
| 3032 | String name; |
no test coverage detected