雷布斯的系统果然名不虚传,各种迷之报错。上传图片的时候发现图片的路径返回值与其他的手机不一样,所以封装了下面的工具来解决MIUI下无法获取到系统相册中的图片的问题。
/*** 小米、红米手机系列获取相册图片* veToFirst()' on a null object reference* 工具处理类,处理小米手机各种ContentProvider空指针*/
public class MIUIPictureUtils {@SuppressLint("NewApi")public static String getPath(final Context context, final Uri uri) {final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;// DocumentProvider if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {// ExternalStorageProviderif (isExternalStorageDocument(uri)) {final String docId = DocumentId(uri);final String[] split = docId.split(":");final String type = split[0];if ("primary".equalsIgnoreCase(type)) {ExternalStorageDirectory() + "/" + split[1];}}// DownloadsProviderelse if (isDownloadsDocument(uri)) {final String id = DocumentId(uri);final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),Long.valueOf(id));return getDataColumn(context, contentUri, null, null);}// MediaProviderelse if (isMediaDocument(uri)) {final String docId = DocumentId(uri);final String[] split = docId.split(":");final String type = split[0];Uri contentUri = null;if ("image".equals(type)) {contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;} else if ("video".equals(type)) {contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;} else if ("audio".equals(type)) {contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;}final String selection = "_id=?";final String[] selectionArgs = new String[]{split[1]};return getDataColumn(context, contentUri, selection,selectionArgs);}}// MediaStore (and general)else if ("content".Scheme())) {// Return the remote addressif (isGooglePhotosUri(uri))LastPathSegment();return getDataColumn(context, uri, null, null);}// Fileelse if ("file".Scheme())) {Path();}return null;}private static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {Cursor cursor = null;final String column = "_data";final String[] projection = {column};try {cursor = ContentResolver().query(uri, projection,selection, selectionArgs, null);if (cursor != null && veToFirst()) {final int index = ColumnIndexOrThrow(column);String(index);}} finally {if (cursor != null)cursor.close();}return null;}private static boolean isExternalStorageDocument(Uri uri) {return "alstorage.documents".Authority());}private static boolean isDownloadsDocument(Uri uri) {return "com.android.providers.downloads.documents".Authority());}private static boolean isMediaDocument(Uri uri) {return "com.dia.documents".Authority());}private static boolean isGooglePhotosUri(Uri uri) {return le.android.t".Authority());}
}
用法:
在onActivityResult中使用:
@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {ActivityResult(requestCode, resultCode, data);String picPath = "";if (BaseUtils.isMIUI()) {//如果是MIUI系统,单独调用获取图片地址的方法Uri uri = Data();picPath = Path(XXXXXX.this, uri);} else {picPath = BitmapFilePath(requestCode, resultCode, data);}Bitmap bmp = BitmapByOpt(picPath);...}
判断是否是MIUI的方法:
public static boolean isMIUI() {try {final BuildProperties prop = wInstance();Property(KEY_MIUI_VERSION_CODE, null) != null|| Property(KEY_MIUI_VERSION_NAME, null) != null|| Property(KEY_MIUI_INTERNAL_STORAGE, null) != null;} catch (final IOException e) {return false;}}
上面的BuildProperties类:
public class BuildProperties {private final Properties properties;private BuildProperties() throws IOException {properties = new Properties();properties.load(new FileInputStream(new RootDirectory(), "build.prop")));}public boolean containsKey(final Object key) {ainsKey(key);}public boolean containsValue(final Object value) {ainsValue(value);}public Set<Map.Entry<Object, Object>> entrySet() {Set();}public String getProperty(final String name) {Property(name);}public String getProperty(final String name, final String defaultValue) {Property(name, defaultValue);}public boolean isEmpty() {return properties.isEmpty();}public Enumeration<Object> keys() {return properties.keys();}public Set<Object> keySet() {return properties.keySet();}public int size() {return properties.size();}public Collection<Object> values() {return properties.values();}public static BuildProperties newInstance() throws IOException {return new BuildProperties();}
}
本文发布于:2024-02-01 12:31:19,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170676188136611.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |