How can I pass a Bitmap object from one activity to another

前端 未结 10 1158
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 03:39

In my activity, I create a Bitmap object and then I need to launch another Activity, How can I pass this Bitmap object from the sub-ac

10条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-22 04:00

    Because Intent has size limit . I use public static object to do pass bitmap from service to broadcast ....

    public class ImageBox {
        public static Queue mQ = new LinkedBlockingQueue(); 
    }
    

    pass in my service

    private void downloadFile(final String url){
            mExecutorService.submit(new Runnable() {
                @Override
                public void run() {
                    Bitmap b = BitmapFromURL.getBitmapFromURL(url);
                    synchronized (this){
                        TaskCount--;
                    }
                    Intent i = new Intent(ACTION_ON_GET_IMAGE);
                    ImageBox.mQ.offer(b);
                    sendBroadcast(i);
                    if(TaskCount<=0)stopSelf();
                }
            });
        }
    

    My BroadcastReceiver

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                LOG.d(TAG, "BroadcastReceiver get broadcast");
    
                String action = intent.getAction();
                if (DownLoadImageService.ACTION_ON_GET_IMAGE.equals(action)) {
                    Bitmap b = ImageBox.mQ.poll();
                    if(b==null)return;
                    if(mListener!=null)mListener.OnGetImage(b);
                }
            }
        };
    

提交回复
热议问题