美文网首页
Android Path 和 Uri 互转

Android Path 和 Uri 互转

作者: 清朝公主大清正黄旗 | 来源:发表于2022-03-15 19:49 被阅读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);
    

    相关文章

      网友评论

          本文标题:Android Path 和 Uri 互转

          本文链接:https://www.haomeiwen.com/subject/pvwydrtx.html