[英]Creating Bitmap using Picasso
I am creating bitmap using below code : 我正在使用以下代码创建位图:
public Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return bitmap;
} catch (Exception e) {
// Log exception
return null;
}
But, it takes more time to load. 但是,加载需要更多时间。 I want to know that is it possible with Picasso library ?
我想知道毕加索图书馆有可能吗?
So far, I have tried using Picasso as below : 到目前为止,我已经尝试过使用Picasso,如下所示:
public Bitmap getBitmapFromURL(String src) {
try {
Picasso.with(this)
.load(src)
.placeholder(R.mipmap.ic_launcher).into(image);
image.setDrawingCacheEnabled(true);
image.buildDrawingCache();
Bitmap bitmap = image.getDrawingCache();
return bitmap;
} catch (Exception e) {
// Log exception
return null;
}
where, 哪里,
image is my imageview inside xml layout. image是我在XML布局内的imageview。 The Problem is that I don't have to show or display ImageView and directly have to generate Bitmap for it if possible with the use of Picasso library.
问题是我不必显示或显示ImageView,并且如果可能的话,可以使用Picasso库直接为其生成Bitmap。
and one more thing to say that I can't execute the code using above way with Picasso. 还有一点要说的是,我无法通过Picasso使用上述方式执行代码。 It gives me error Illegal state exception .
它给我错误非法状态异常 。
Please check this 请检查一下
private Target target = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
//get bitmap
}
@Override
public void onBitmapFailed() {
}
}
private void someMethod() {
Picasso.with(this).load("url").into(target);
}
@Override
public void onDestroy() { // could be in onPause or onStop
Picasso.with(this).cancelRequest(target);
super.onDestroy();
}
target = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
// bitmap instance and use it for your own
}
@Override
public void onBitmapFailed() {
}
};
Picasso.with(this).load(yourImageURL).into(target);
Use the above code to obtain this. 使用上面的代码来获取它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.