Android RemoteViews: setImageViewResource() vs setImageViewUri() vs setImageViewBitmap()

前端 未结 2 1375
太阳男子
太阳男子 2021-01-25 13:07

The following 3 methods (from RemoteViews class) can be used to update an image on a Home Screen widget, on Android:

setImageViewResource (int viewI         


        
相关标签:
2条回答
  • 2021-01-25 13:41

    Some people recommend using setImageViewUri() to reduce the risk of freezing the widgets on the Home Screen. See https://code.google.com/p/android/issues/detail?id=28216#c102

    0 讨论(0)
  • 2021-01-25 13:54

    The Binder transaction failed because it was too large. During a remote procedure call, the arguments and the return value of the call are transferred as Parcel objects stored in the Binder transaction buffer. If the arguments or the return value are too large to fit in the transaction buffer, then the call will fail and TransactionTooLargeException will be thrown. The Binder transaction buffer has a limited fixed size, currently 1Mb, which is shared by all transactions in progress for the process. Consequently this exception can be thrown when there are many transactions in progress even when most of the individual transactions are of moderate size. There are two possible outcomes when a remote procedure call throws TransactionTooLargeException. Either the client was unable to send its request to the service (most likely if the arguments were too large to fit in the transaction buffer), or the service was unable to send its response back to the client (most likely if the return value was too large to fit in the transaction buffer). It is not possible to tell which of these outcomes actually occurred. The client should assume that a partial failure occurred. The key to avoiding TransactionTooLargeException is to keep all transactions relatively small. Try to minimize the amount of memory needed to create a Parcel for the arguments and the return value of the remote procedure call. Avoid transferring huge arrays of strings or large bitmaps. If possible, try to break up big requests into smaller pieces. If you are implementing a service, it may help to impose size or complexity contraints on the queries that clients can perform. For example, if the result set could become large, then don't allow the client to request more than a few records at a time. Alternately, instead of returning all of the available data all at once, return the essential information first and make the client ask for additional information later as needed.

    It's possible if you REALLY want to do it, but it costs a lot of memory and is also slow. It might not work if you have an older phone and a big bitmap. You could just pass it as an extra, for example intent.putExtra("data", bitmap). A Bitmap implements Parcelable, so you can put it in an extra and set it using setImageViewBitmap (int viewId, Bitmap bitmap).

    If you want to pass it in between activities, I would store it in a file. That's more efficient, and less work for you. You can create private files in your data folder usingMODE_PRIVATE that are not accessible to any other app.

    If you pass it as a Parcelable, you're bound to get a JAVA BINDER FAILURE error. So, the solution is this: If the bitmap is small, like, say, a thumbnail, pass it as a byte array and build the bitmap for display in the next activity. For instance:

    in your calling activity...

    Intent i = new Intent(this, NextActivity.class);
    Bitmap b; // your bitmap
    ByteArrayOutputStream bs = new ByteArrayOutputStream();
    b.compress(Bitmap.CompressFormat.PNG, 50, bs);
    i.putExtra("byteArray", bs.toByteArray());
    startActivity(i);
    

    ...and in your receiving activity

    if(getIntent().hasExtra("byteArray")) {
        ImageView previewThumbnail = new ImageView(this);
        Bitmap b = BitmapFactory.decodeByteArray(
          getIntent().getByteArrayExtra("byteArray"),0,getIntent().getByteArrayExtra("byteArray").length);        
    previewThumbnail.setImageBitmap(b);
    }
    
    0 讨论(0)
提交回复
热议问题