Android Path 和 Uri 互转

2022-03-15  本文已影响0人  清朝公主大清正黄旗

1、path 转 Uri
1)、

public static Uri getImageStreamFromExternal(String imageName) {
        File externalPubPath = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES
        );
        File picPath = new File(externalPubPath, imageName);
        Uri uri = null;
        if(picPath.exists()) {
             uri = Uri.fromFile(picPath);
        }
        return uri;
    }

2)、

public static Uri getMediaUriFromPath(Context context, String path) {
        Uri mediaUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        Cursor cursor = context.getContentResolver().query(mediaUri,
                null,
                MediaStore.Images.Media.DISPLAY_NAME + "= ?",
                new String[] {path.substring(path.lastIndexOf("/") + 1)},
                null);
        Uri uri = null;
        if(cursor.moveToFirst()) {
            uri = ContentUris.withAppendedId(mediaUri,
                    cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media._ID)));
        }
        cursor.close();
        return uri;
    }

1、通过Uri 直接获取Bitmap

Bitmap bitmap = MediaStore.Images.Media.getBitmap( contentResolver, uri);
上一篇下一篇

猜你喜欢

热点阅读