我正在尝试使用Universal Image Loader库获取位图资源。我已经覆盖了onLoadComplete()方法,如下所示:
loader.displayImage(thumbnail.get(position), hold.ivThumbnailReflection, options, new ImageLoadingListener() {
@Override
public void onLoadingStarted(String arg0, View arg1) {
// TODO Auto-generated method stub
}
@Override
public void onLoadingFailed(String arg0, View arg1, FailReason arg2) {
// TODO Auto-generated method stub
}
@Override
public void onLoadingComplete(String urlLink, View arg1, Bitmap loadedImage) {
Log.i("loading complete","loading complete "+loadedImage);
hold.ivThumbnail.setImageBitmap(loadedImage);
}
@Override
public void onLoadingCancelled(String arg0, View arg1) {
// TODO Auto-generated method stub
}
});
图片在ivThumbnailReflection
视图中设置,但ivThumbnail
视图中没有图片。当我为loadedImage打印对象时,我得到一个表格 - android.graphics.Bitmap@4170f1d0。
我做错了什么?请帮助。
编辑:初始化UIL的选项:
options = new DisplayImageOptions.Builder()
.cacheOnDisc(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.imageScaleType(ImageScaleType.IN_SAMPLE_INT)
.build();
答案 0 :(得分:10)
为什么不使用此而不是使用完整的:
imageLoader.displayImage(imageUri, imageView);
或者使用此
imageLoader.loadImage(imageUri, new SimpleImageLoadingListener()
{
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage)
{
// Do whatever you want with Bitmap
}
});
答案 1 :(得分:5)
尝试使用loadImageSync(String uri)
。它为您提供位图。
如果你想从位图中抽取
Drawable d = new BitmapDrawable(getResources(),imageLoader.loadImageSync(url);
答案 2 :(得分:1)
此代码必须帮助您
public class CreateNotificationActivity extends AppCompatActivity {
private NetworkImageView mNetworkImageView;
private ImageLoader mImageLoader;
Bitmap bit;
String url = "http://www.whitegadget.com/attachments/pc-wallpapers/145032d1392006652-nature-wallpaper-nature-picture.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_notification);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mNetworkImageView = (NetworkImageView) findViewById(R.id.networkImageView1);
//bit = BitmapFactory.decodeResource(getResources(), R.drawable.bgd);
//Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bgd);
mImageLoader = CustomVolleyRequestQueue.getInstance(this.getApplicationContext())
.getImageLoader();
Bitmap bitmap =mImageLoader.get(url, ImageLoader.getImageListener(mNetworkImageView,
R.mipmap.ic_launcher, R.drawable.icon)).getBitmap();
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
createNotification();
}
});
}
@Override
protected void onStart() {
super.onStart();
mImageLoader = CustomVolleyRequestQueue.getInstance(this.getApplicationContext())
.getImageLoader();
bit =mImageLoader.get(url, ImageLoader.getImageListener(mNetworkImageView,
R.mipmap.ic_launcher, R.drawable.icon)).getBitmap();
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void createNotification()
{
bit = BitmapFactory.decodeResource(getResources(), R.drawable.bgd);
mImageLoader.get(url, new ImageLoader.ImageListener() {
@Override
public void onResponse(ImageLoader.ImageContainer imageContainer, boolean b) {
bit =mImageLoader.get(url, ImageLoader.getImageListener(mNetworkImageView,
R.mipmap.ic_launcher, android.R.drawable
.ic_dialog_alert)).getBitmap();
Intent intent = new Intent(getApplicationContext(),NotificationReceiverActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(getApplicationContext(), (int) System.currentTimeMillis(), intent, 0);
Notification noti = new Notification.Builder(getApplicationContext())
.setContentTitle("Mail Notification")
.setContentText("5 new unread messages")
.setSmallIcon(R.drawable.bgd)
.setStyle(new Notification.BigPictureStyle().bigPicture(bit))
.setAutoCancel(true)
.setContentIntent(pIntent).build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0,noti);
Log.i("LOG>>>>>>>>>>>>>>>>>>>","inside the on response");
}
@Override
public void onErrorResponse(VolleyError volleyError) {
bit = BitmapFactory.decodeResource(getResources(), R.drawable.bgd);
Log.i("LOG>>>>>>>>>>>>>>>>>>>","inside the errror response");
}
});
// bit =mImageLoader.get(url, ImageLoader.getImageListener(mNetworkImageView,
// R.mipmap.ic_launcher, android.R.drawable
// .ic_dialog_alert)).getBitmap();
}
}
答案 3 :(得分:0)
尝试使用Handler或使用runOnUiThread(..)方法在ui线程中设置位图,如下所示:
handler..post(new Runnable() { // handler must be initialized in UI thread
@Override
public void run() {
hold.ivThumbnail.setImageBitmap(loadedImage);
}
});
或者尝试使用runOnUiThread()方法:
runOnUiThread(new Runnable() {
@Override
public void run() {
hold.ivThumbnail.setImageBitmap(loadedImage);
}
});
答案 4 :(得分:0)
确保在主/ UI线程中创建选项。
否则,使用处理程序分配扩展您options
的初始化。
options = new DisplayImageOptions.Builder()
.cacheOnDisc(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.imageScaleType(ImageScaleType.IN_SAMPLE_INT)
.handler(yourHandler)
.build();
其中yourHandler
应该在主/ UI线程中创建。