如何从Bitmap获取Uri对象

Dar*_*kar 24 android uri bitmap

在某个点击事件中,我要求用户添加图像.所以我提供两种选择:

  1. 要从库中添加.
  2. 从相机中单击新图像.

我的目标是保留与这些图像相关的"uri"列表.

如果用户选择了图库,那么我会得到图像uri(这非常简单).但如果他选择相机,那么在拍完照片之后,我就会得到那张照片的Bitmap对象.

现在我如何将该Bitmap对象转换为uri,或者换句话说,如何获取该位图对象的相对Uri对象?

谢谢.

Aja*_*jay 43

我的项目有同样的问题,所以我按照简单的方法(点击这里)从位图获取Uri.

public Uri getImageUri(Context inContext, Bitmap inImage) {
  ByteArrayOutputStream bytes = new ByteArrayOutputStream();
  inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
  String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
  return Uri.parse(path);
} 
Run Code Online (Sandbox Code Playgroud)

  • 它在android 6.0中返回null路径 (11认同)
  • `Images.Media.insertImage`现在是`MediaStore.Images.Media.insertImage`你需要记住添加这个权限的第二件事:`<uses-permission android:name ="android.permission.WRITE_EXTERNAL_STORAGE"/>` (5认同)
  • 如何从存储中删除此插入的位图文件? (3认同)

Han*_*nan 6

Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
Run Code Online (Sandbox Code Playgroud)

上面提到的这一行使用位图创建一个缩略图,这可能会占用Android设备中的一些额外空间.

此方法可以帮助您从位图获取Uri而不消耗额外的内存.

public Uri bitmapToUriConverter(Bitmap mBitmap) {
   Uri uri = null;
   try {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, 100, 100);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    Bitmap newBitmap = Bitmap.createScaledBitmap(mBitmap, 200, 200,
            true);
    File file = new File(getActivity().getFilesDir(), "Image"
            + new Random().nextInt() + ".jpeg");
    FileOutputStream out = getActivity().openFileOutput(file.getName(),
            Context.MODE_WORLD_READABLE);
    newBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
    out.flush();
    out.close();
    //get absolute path
    String realPath = file.getAbsolutePath();
    File f = new File(realPath);
    uri = Uri.fromFile(f);

  } catch (Exception e) {
    Log.e("Your Error Message", e.getMessage());
  }
return uri;
}


public static int calculateInSampleSize(
        BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) >= reqHeight
                && (halfWidth / inSampleSize) >= reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}
Run Code Online (Sandbox Code Playgroud)

有关更多详细信息,请高效地加载大位图


Vis*_*yas 0

我尝试了我在评论中提到的帖子中的以下代码片段..它对我来说工作得很好。

/**
 * Gets the last image id from the media store
 * 
 * @return
 */
private int getLastImageId() {
    final String[] imageColumns = { MediaStore.Images.Media._ID,
            MediaStore.Images.Media.DATA };
    final String imageOrderBy = MediaStore.Images.Media._ID + " DESC";
    Cursor imageCursor = managedQuery(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageColumns,
            null, null, imageOrderBy);
    if (imageCursor.moveToFirst()) {
        int id = imageCursor.getInt(imageCursor
                .getColumnIndex(MediaStore.Images.Media._ID));
        String fullPath = imageCursor.getString(imageCursor
                .getColumnIndex(MediaStore.Images.Media.DATA));
        Log.d(getClass().getSimpleName(), "getLastImageId::id " + id);
        Log.d(getClass().getSimpleName(), "getLastImageId::path "
                + fullPath);
        imageCursor.close();
        return id;
    } else {
        return 0;
    }
}
Run Code Online (Sandbox Code Playgroud)

logcat 中的输出:

09-24 16:36:24.500: getLastImageId::id 70
09-24 16:36:24.500: getLastImageId::path /mnt/sdcard/DCIM/Camera/2012-09-24 16.36.20.jpg
Run Code Online (Sandbox Code Playgroud)

另外,我在上面的代码片段中没有看到任何编码名称。希望这可以帮助。