gpt4 book ai didi

Android 从路径获取图像(图库、图片等)

转载 作者:行者123 更新时间:2023-11-30 00:52:23 26 4
gpt4 key购买 nike

需要从路径获取图像。我已经尝试了所有方法,但似乎没有得到图像。

我的两个图片路径:

/storage/emulated/0/DCIM/Camera/20161025_081413.jpg
content://media/external/images/media/4828

如何从这些路径设置我的图像?
我正在使用 ImageView 来显示我的图像。

我的代码:

File imgFile = new File("/storage/emulated/0/DCIM/Camera/20161025_081413.jpg");
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
holder.myimage.setImageBitmap(myBitmap);

提前致谢

最佳答案

一般情况下,你可以只写BitmapFactory.decodeBitmap(....)等,但是文件可能很大,你很快就会得到OutOfMemoryError,特别是,如果您连续解码几次。所以在设置为查看之前需要先压缩图片,这样才不会耗尽内存。这是正确的方法。

File f = new File(path);
if(file.exists()){
Bitmap myBitmap = ImageHelper.getCompressedBitmap(photoView.getMaxWidth(), photoView.getMaxHeight(), f);
photoView.setImageBitmap(myBitmap);
}

//////////////

/**
* Compresses the file to make a bitmap of size, passed in arguments
* @param width width you want your bitmap to have
* @param height hight you want your bitmap to have.
* @param f file with image
* @return bitmap object of sizes, passed in arguments
*/
public static Bitmap getCompressedBitmap(int width, int height, File f) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(f.getAbsolutePath(), options);

options.inSampleSize = calculateInSampleSize(options, width, height);
options.inJustDecodeBounds = false;

return BitmapFactory.decodeFile(f.getAbsolutePath(), options);
}

////////////////

  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;
}

关于Android 从路径获取图像(图库、图片等),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40768268/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com