我使用以下代码下载图像:
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。而且图像看起来更大。我该如何解决?