Android 如何使用毕加索或通用安卓加载器检索位图?
这是我的actuel代码:Android 如何使用毕加索或通用安卓加载器检索位图?,android,bitmap,universal-image-loader,picasso,Android,Bitmap,Universal Image Loader,Picasso,这是我的actuel代码: public Bitmap downloadBitmap(String url) { Bitmap bitmap = null; // initilize the default HTTP client object final DefaultHttpClient client = new DefaultHttpClient(); //forming a HttpGet request final HttpGet ge
public Bitmap downloadBitmap(String url) {
Bitmap bitmap = null;
// initilize the default HTTP client object
final DefaultHttpClient client = new DefaultHttpClient();
//forming a HttpGet request
final HttpGet getRequest = new HttpGet(url);
try {
HttpResponse response = client.execute(getRequest);
//check 200 OK for success
final int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
Log.w("ImageDownloader", "Error " + statusCode +
" while retrieving bitmap from " + url);
return null;
}
final HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream inputStream = null;
try {
// getting contents from the stream
inputStream = entity.getContent();
// decoding stream data back into image Bitmap that android understands
bitmap = BitmapFactory.decodeStream(inputStream);
inputStream.reset();
} finally {
if (inputStream != null) {
inputStream.close();
}
entity.consumeContent();
}
}
} catch (Exception e) {
Log.e("ImageDownloader", "Something went wrong while" +
" retrieving bitmap from " + url + e.toString());
}
return bitmap;
}`
我想在我的函数中做同样的事情,但是使用毕加索库或通用图像加载器。这些库有没有办法只返回带有url的位图
谢谢这两个问题都可以在本网站上得到解答 使用毕加索:
private Target mTarget = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
// Do whatever you want with the Bitmap
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
}
...
Picasso.with(this).load("url").into(mTarget);
至少在请求进行期间,您必须保留对目标
实例的引用。您可以稍后通过调用cancelRequest()
取消加载
使用UIL:
imageLoader.loadImage(imageUri, new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
// Do whatever you want with the Bitmap
}
});
这两个问题都可以,而且都已经在这个网站上得到了回答 使用毕加索:
private Target mTarget = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
// Do whatever you want with the Bitmap
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
}
...
Picasso.with(this).load("url").into(mTarget);
至少在请求进行期间,您必须保留对目标
实例的引用。您可以稍后通过调用cancelRequest()
取消加载
使用UIL:
imageLoader.loadImage(imageUri, new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
// Do whatever you want with the Bitmap
}
});
你必须在毕加索那里保留对
目标的引用,否则它将被垃圾收集。如果你是在后台线程上,你也可以调用.get()
获取位图
。谢谢你,杰克,我从来没有使用过这个功能,但事实上我刚刚在文档中看到了这一点。我正在编辑答案。你必须与毕加索一起保留对目标的引用,否则它将被垃圾收集。如果你是在后台线程上,你也可以调用.get()
获取位图
。谢谢你,杰克,我从来没有使用过这个功能,但事实上我刚刚在文档中看到了这一点。我正在编辑答案。