2

我正在尝试使用通用图像加载器库获取位图资源。我已经像这样覆盖了 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。当我打印加载图像的对象时,我得到一个格式为 android.graphics.Bitmap@4170f1d0 的字符串。

我究竟做错了什么?请帮忙。

编辑:初始化 UIL 的选项:

options = new DisplayImageOptions.Builder()
.cacheOnDisc(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.imageScaleType(ImageScaleType.IN_SAMPLE_INT)
.build();
4

5 回答 5

10

你为什么不只使用这个而不是使用 complete :

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
    }
});
于 2013-08-13T05:52:02.500 回答
5

尝试使用loadImageSync(String uri). 它为您提供位图。

如果你想从位图中绘制

Drawable d = new BitmapDrawable(getResources(),imageLoader.loadImageSync(url);
于 2014-02-25T22:57:59.807 回答
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();

    }

}
于 2016-01-20T04:24:51.607 回答
0

确保在主/UI 线程中创建选项。

options否则,使用处理程序分配扩展您的初始化。

options = new DisplayImageOptions.Builder()
  .cacheOnDisc(true)
  .bitmapConfig(Bitmap.Config.RGB_565)
  .imageScaleType(ImageScaleType.IN_SAMPLE_INT)
  .handler(yourHandler)
  .build();

yourHandler应该在主/UI线程中创建哪里。

于 2013-08-13T06:17:23.243 回答
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);
            }
        });
于 2013-08-13T05:50:15.100 回答