【问题标题】:Converting android image URI转换 android 图像 URI
【发布时间】:2013-02-12 20:30:25
【问题描述】:

我得到了以下类型的 uri:

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

如何将以上类型的 URI 转换为以下 URI:

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

请帮忙 谢谢

【问题讨论】:

标签: android uri


【解决方案1】:
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;
        }
    }
}

【讨论】:

  • imageFile.exists() 后面的代码是关于什么的?它会将图像复制到外部文件夹吗?
  • 我已使用此代码,但它不适用于我的 android 5.1 模拟器。
猜你喜欢
  • 2019-11-15
  • 1970-01-01
  • 2013-07-31
  • 1970-01-01
  • 1970-01-01
  • 2017-05-04
  • 1970-01-01
  • 2016-02-13
  • 1970-01-01
相关资源
最近更新 更多