Android的转换图像URI [英] Converting android image URI

查看:123
本文介绍了Android的转换图像URI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到以下类型的URI的是:

  /mnt/sdcard/Pictures/WW/ww​​_1360248819300.jpg

如何上述类型的URI转换为以下网址:

 内容://媒体/外部/图像/媒体/ 12

请帮忙
谢谢


解决方案

 公共静态乌里getImageContentUri(上下文的背景下,文件镜像文件){
    串文件路径= imageFile.getAbsolutePath();
    光标光标= context.getContentResolver()查询(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            新的String [] {} MediaStore.Images.Media._ID,
            MediaStore.Images.Media.DATA +=?,
            新的String [] {}文件路径,NULL);
    如果(光标=空&放大器;!&放大器; cursor.moveToFirst()){
        INT ID = cursor.getInt(光标
                .getColumnIndex(MediaStore.MediaColumns._ID));
        乌里基本URI = Uri.parse(内容://媒体/外部/图像/媒体);
        返回Uri.withAppendedPath(基本URI,+ ID);
    }其他{
        如果(imageFile.exists()){
            ContentValues​​值=新ContentValues​​();
            values​​.put(MediaStore.Images.Media.DATA,文件路径);
            返回context.getContentResolver()。插入(
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI,价值观);
        }其他{
            返回null;
        }
    }
}

i am getting below type of uri:

 /mnt/sdcard/Pictures/WW/ww_1360248819300.jpg

how to convert above type of URI to below URI:

 content://media/external/images/media/12

please help Thanks

解决方案

public static Uri getImageContentUri(Context context, File imageFile) {
    String filePath = imageFile.getAbsolutePath();
    Cursor cursor = context.getContentResolver().query(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            new String[] { MediaStore.Images.Media._ID },
            MediaStore.Images.Media.DATA + "=? ",
            new String[] { filePath }, null);
    if (cursor != null && cursor.moveToFirst()) {
        int id = cursor.getInt(cursor
                .getColumnIndex(MediaStore.MediaColumns._ID));
        Uri baseUri = Uri.parse("content://media/external/images/media");
        return Uri.withAppendedPath(baseUri, "" + id);
    } else {
        if (imageFile.exists()) {
            ContentValues values = new ContentValues();
            values.put(MediaStore.Images.Media.DATA, filePath);
            return context.getContentResolver().insert(
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        } else {
            return null;
        }
    }
}

这篇关于Android的转换图像URI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆