简体   繁体   English

如何将位图从服务发送到Java,Android中的main?

[英]How can I send bitmap from service to a main in java, android?

I'm making a service in my application to download images, that's unclear for me how can i send result (bitmap) from service to a main activity. 我正在应用程序中提供服务以下载图像,这对我来说还不清楚如何将结果(位图)从服务发送到主要活动。 Can I do that? 我可以那样做吗? Or in service there is only a possibitlity to save downloaded image somewhere and send only useless messages? 还是在使用中,只能将下载的图像保存在某处并仅发送无用的消息?

Try binding from the activity to the service and then pass the instance of the activity to the service. 尝试从活动绑定到服务,然后将活动实例传递给服务。 In this case you can pass bitmap from a service to the activity. 在这种情况下,您可以将位图从服务传递到活动。 However, you need to be real careful not introduce memory leaks 但是,您需要非常小心,不要引入内存泄漏

Something like this: 像这样:

class TestActivity extends Activity {
    private BitmapService mBitmapService;
    private final ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName,
                                       IBinder iBinder) {
            BitmapService.BitmapServiceBinder mBinder = (BitmapService.BitmapServiceBinder) iBinder;
            mBitmapService = mBinder.getBitmapService();
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
        }
    };

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        bindService(new Intent(this, XMLDownloaderService.class), mServiceConnection, BIND_AUTO_CREATE);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(this.mServiceConnection);
    }
}

Your Service Side: 您的服务端:

public class BitmapService extends BitmapService {

    public class BitmapServiceBinder extends Binder {
        public BitmapService getBitmapService() {
             return BitmapService.this;
        }
    }

     public BitmapService() {
        super("BitmapService");
     }

     @Override
     public IBinder onBind(Intent intent) {
         return mBinder;
     }

     @Override
     public void onCreate() {
          super.onCreate();
           if (mBinder == null) {
               mBinder = new BitmapServiceBinder();
           }
     }
}

Bitmap implements Parcelable , so you could always pass it in the intent: Bitmap实现了Parcelable ,因此您可以始终按意图传递它:

Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("BitmapImage", bitmap);

and retrieve it on the other end: 并在另一端检索它:

Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");

If the bitmap exists as a file or a resource, its is always better to pass the URI or ResourceID of the bitmap and not the bitmap itself. 如果位图以文件或资源的形式存在,则最好传递位图的URI或ResourceID,而不是位图本身。 Passing the entire bitmap requires a lot of memory. 传递整个位图需要大量内存。 Passing the URL requires very little memory and allows each activity to load and scale the bitmap as they need it. 传递URL所需的内存很少,并且允许每个活动根据需要加载和缩放位图。

Answer pullout from: How can I pass a Bitmap object from one activity to another 答案来自: 如何将位图对象从一个活动传递到另一个活动

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Java中的Android上重复位图? - How can i repeat bitmap on Android in Java? 如何从Java的后台服务发送POST请求? - How can I send POST requests from a background service in Java? 如何将图像从Java应用程序发送到Android应用程序? - How can I send an Image from a Java application to an Android application? 如何将数据从线程发送到主要活动 - how to send data from thread to main activity java android 如何从 Android 内存中完全清除位图? - How can I completely clear a bitmap from memory on Android? 当我从Android向Flask Web服务发送参数时,如何解决“ SSL库故障”? - How can solve “Failure in SSL library” when I send params to a Flask web service from Android? 如何修改NDK中的Android位图,以便在Java端使用它? - How can I modify an Android bitmap in the NDK so that I can use it on the Java side? Android如何获取屏幕快照位图? - Android how can I get screenshot bitmap? 如何避免java.lang.OutOfMemoryError:在使用大位图时,位图大小超过了android中的VM预算? - How can I avoid java.lang.OutOfMemoryError: bitmap size exceeds VM budget in android while using a large bitmap? 如何将整数从Java文件发送到XML文件夹? (Android Studio) - How can I send a integer from my Java file to my XML folder? (Android Studio)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM