gpt4 book ai didi

java - 如何显示具有路径的图像?

转载 作者:行者123 更新时间:2023-11-29 09:06:58 26 4
gpt4 key购买 nike

我正在尝试显示具有绝对路径的图像。我在 stackoverflow 上发现了这段代码,理论上它应该可以工作,但是我在大多数图像上收到错误 Bitmap too be uploaded into a texture 所以我正在寻找另一种方法来做到这一点。令人惊讶的是,除了这个例子之外,没有任何关于如何做到这一点的例子。

这就是我正在尝试的:

Bitmap myBitmap = BitmapFactory.decodeFile(imagePath);                  
ImageView image = new ImageView(context);
image.setImageBitmap(myBitmap);
layout.addView(image);

顺便说一句,我正在使用的图像是使用默认相机应用程序拍摄的,因此它们没有任何不常见的格式或大小(并且可以在图库应用程序中毫无问题地看到)。如何将它们添加到我的布局中?

最佳答案

首先尝试使用以下代码调整图像大小,然后将其设置到 ImageView 中:

 public static Drawable GetDrawable(String newFileName)
{
File f;
BitmapFactory.Options o2;
Bitmap drawImage = null;
Drawable d = null;
try
{
f = new File(newFileName);
//decodes image and scales it to reduce memory consumption
//Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
o.inTempStorage = new byte[16 * 1024];
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
//The new size we want to scale to
final int REQUIRED_SIZE = 150;
//Find the correct scale value. It should be the power of 2.
int scale = 1;
while ((o.outWidth / scale / 2 >= REQUIRED_SIZE) && (o.outHeight / scale / 2 >= REQUIRED_SIZE))
scale *= 2;
//Decode with inSampleSize
o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
drawImage = BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
//Bitmap bmp = pictureDrawableToBitmap((PictureDrawable) drawable);
d = new BitmapDrawable(drawImage);
//drawImage.recycle();
//new BitmapWorkerTask
}
catch (FileNotFoundException e)
{
}
return d;
}

使用上面的方法如下:

imageView.setImageBitmap(myBitmap);

关于java - 如何显示具有路径的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14433014/

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