问题描述
我收到一个代表我的图像的 InputStream
,我尝试通过 Picasso 将它加载到 ImageView
。
public static Picasso getCustomPicasso(InputStream fileInputStream,Context context) {
return new Picasso.Builder(context)
.listener((picasso,uri,exception) -> exception.printstacktrace())
.addRequestHandler(new RequestHandler() {
@Override
public boolean canHandleRequest(Request data) {
return true;
}
@Override
public Result load(Request request,int networkPolicy) {
return new Result(BitmapFactory.decodeStream(fileInputStream),Picasso.LoadedFrom.NETWORK);
}
}).build();
}
getCustomPicasso(fileInputStream,context)
.load(fileName)
.resize(0,720)
.into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap,Picasso.LoadedFrom from) {
Logger.d(TAG,"Image loaded from the network.");
imageView.setimageBitmap(bitmap);
}
@Override
public void onBitmapFailed(Exception e,Drawable errorDrawable) {
Logger.e(TAG,"Image load from the network Failed: " + e.getMessage());
e.printstacktrace();
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
一般情况下它可以正常工作,但如果图像尺寸大/分辨率高,则图像不会加载到 ImageView
。我还尝试重新调整 Bitmap
的大小,就像 here 所描述的那样。我在 decodeSampledBitmapFromInputStream
的 load
方法中调用 RequestHandler
方法而不是 BitmapFactory.decodeStream(fileInputStream)
,它返回 null
。
public static Picasso getCustomPicasso(InputStream fileInputStream,int networkPolicy) {
return new Result(decodeSampledBitmapFromInputStream(fileInputStream,100,100),Picasso.LoadedFrom.NETWORK);
}
}).build();
}
public static Bitmap decodeSampledBitmapFromInputStream(InputStream fileInputStream,int reqWidth,int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res,resId,options);
BitmapFactory.decodeStream(fileInputStream,null,options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options,reqWidth,reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
//return BitmapFactory.decodeResource(res,options);
return BitmapFactory.decodeStream(fileInputStream,options);
}
private static int calculateInSampleSize(BitmapFactory.Options options,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;
}
但在这种情况下,我收到 null
Bitmap
并且根本不显示任何图像。我想知道如何解决这个问题。任何帮助将不胜感激。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)