Android 从URL加载时获取一半图像
我正在从URL加载图像,并在运行时显示在imageview上 我的问题是有时我的图像只显示图像的一半,而图像的其余部分显示为白色。我在网上读了很多帖子,但没有一篇对我有同样的帮助 请为同样和真正的原因提供一个好的解决方案 下面是我用来创建位图的代码Android 从URL加载时获取一半图像,android,bitmap,bitmapfactory,Android,Bitmap,Bitmapfactory,我正在从URL加载图像,并在运行时显示在imageview上 我的问题是有时我的图像只显示图像的一半,而图像的其余部分显示为白色。我在网上读了很多帖子,但没有一篇对我有同样的帮助 请为同样和真正的原因提供一个好的解决方案 下面是我用来创建位图的代码 public static Bitmap getBitmapFromCard(String path, int reqWidth, int reqHeight) { try { fina
public static Bitmap getBitmapFromCard(String path, int reqWidth,
int reqHeight) {
try {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
BitmapFactory.decodeFile(path, options);
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(path, options);
} catch (OutOfMemoryError e) {
System.gc();
System.gc();
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
BitmapFactory.decodeFile(path, options);
options.inSampleSize = 3;
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(path, 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 heightRatio = Math.round((float) height
/ (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
public static int calculateInSampleSize(BitmapFactory.Options、,
输入要求宽度,输入要求高度){
//图像的原始高度和宽度
最终内部高度=options.outHeight;
最终整数宽度=options.outWidth;
int inSampleSize=1;
如果(高度>要求高度| |宽度>要求宽度){
最终整数高度比=数学圆((浮动)高度
/(浮动)高度);
最终整数宽度比=数学圆((浮动)宽度/(浮动)宽度);
inSampleSize=高度比<宽度比?高度比:宽度比;
}
返回样本大小;
}
附加屏幕截图。您是否检查从url下载时是否通过字节获得完整图像?您是否将下载的图像字节与url图像内容长度进行比较?尝试其他方法,而不是decodeFile decodeStream BitmapFactory.decodeStream(ctx.openInputStream(uri),null,o);如果这种方法不起作用,你可以尝试使用毕加索图书馆。此库用于从中加载图片url@GauravArora能否请您添加更多有关您使用的条件的详细信息?图像有多少字节,您使用的设备是什么?据我所知,你的问题似乎与记忆有关。关于使用System.gc()。