gpt4 book ai didi

android - Drawable.getIntrinsicWidth() 的尺寸错误

转载 作者:太空宇宙 更新时间:2023-11-03 11:44:37 24 4
gpt4 key购买 nike

我用这段代码下载图片:

ImageGetter imageGetter = new ImageGetter() {
@Override
public Drawable getDrawable(String source) {
Drawable drawable = null;
try {
URL url = new URL(source);
String path = Environment.getExternalStorageDirectory().getPath()+"/Android/data/com.my.pkg/"+url.getFile();
File f=new File(path);
if(!f.exists()) {
URLConnection connection = url.openConnection();
InputStream is = connection.getInputStream();

f=new File(f.getParent());
f.mkdirs();

FileOutputStream os = new FileOutputStream(path);
byte[] buffer = new byte[4096];
int length;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
os.close();
is.close();
}
drawable = Drawable.createFromPath(path);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (Throwable t) {
t.printStackTrace();
}
if(drawable != null) {
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
}
return drawable;
}
};

这张图片的尺寸为 20x20。但是 drawable.getIntrinsicWidth() 和 drawable.getIntrinsicHeight() 返回 27。图像看起来更大。我该如何解决?

最佳答案

我尝试了答案中的代码,但没有成功。所以我改用下面的代码,它运行良好。

     DisplayMetrics dm = context.getResources().getDisplayMetrics();

Options options=new Options();
options.inDensity=dm.densityDpi;
options.inScreenDensity=dm.densityDpi;
options.inTargetDensity=dm.densityDpi;

Bitmap bmp = BitmapFactory.decodeFile(path,options);
drawable = new BitmapDrawable(bmp, context.getResources());

关于android - Drawable.getIntrinsicWidth() 的尺寸错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13013986/

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