android - 将位图放入包中

标签 android bitmap bundle parcelable parcel

我想使用 AIDL 将字符串和位图传递给服务。该服务实现了这个 AIDL 方法:

void addButton(in Bundle data);

在我的例子中,Bundle 包含一个字符串和一个位图。

调用应用程序(客户端)有这段代码:

...
// Add text to the bundle
Bundle data = new Bundle();
String text = "Some text";
data.putString("BundleText", text);

// Add bitmap to the bundle
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.myIcon);
data.putParcelable("BundleIcon", icon);

try {
    myService.addButton(data);

} catch (RemoteException e) {
    Log.e(TAG, "Exception: ", e);
    e.printStackTrace();
}
...

在服务端,我有一个包含以下代码的 ButtonComponent 类:

public final class ButtonComponent implements Parcelable {
    private final Bundle mData;

    private ComponComponent(Parcel source) {
        mData = source.readBundle();
    }

    public String getText() {
        return mData.getString("BundleText");
    }

    public Bitmap getIcon() {
        Bitmap icon = (Bitmap) mData.getParcelable("BundleIcon");
        return icon;
    }

    public void writeToParcel(Parcel aOutParcel, int aFlags) {
        aOutParcel.writeBundle(mData);
    }

    public int describeContents() {
        return 0;
    }
}

创建 ButtonComponent 后,服务使用 ButtonComponent 对象中的文本和图标创建按钮:

...
mInflater.inflate(R.layout.my_button, aParent, true);
Button button = (Button) aParent.getChildAt(aParent.getChildCount() - 1);

// Set caption and icon
String caption = buttonComponent.getText();
if (caption != null) {
    button.setText(caption);
}

Bitmap icon = buttonComponent.getIcon();
if (icon != null) {
    BitmapDrawable iconDrawable = new BitmapDrawable(icon);
    button.setCompoundDrawablesWithIntrinsicBounds(iconDrawable, null, null, null);
}
...

结果,按钮显示了正确的文本,我可以看到图标的空间,但没有绘制实际的位图(即文本左侧有一个空白空间)。

这样把Bitmap放到Bundle里对吗?

如果我应该使用 Parcel(相对于 Bundle),是否有任何方法可以在 AIDL 方法中维护单个“数据”参数以将文本和图标保持在一起?

附带问题:我如何决定使用 bundle 还是包裹?

非常感谢。

最佳答案

这是第二个问题的答案。

来源:http://www.anddev.org/general-f3/bundle-vs-parcel-vs-message-t517.html

A Bundle is functionally equivalent to a standard Map. The reason we didn't just use a Map is because in the contexts where Bundle is used, the only things that are legal to put into it are primitives like Strings, ints, and so on. Because the standard Map API lets you insert arbitrary Objects, this would allow developers to put data into the Map that the system can't actually support, which would lead to weird, non-intuitive application errors. Bundle was created to replace Map with a typesafe container that makes it explicitly clear that it only supports primitives.

A Parcel is similar to a Bundle, but is more sophisticated and can support more complex serialization of classes. Applications can implement the Parcelable interface to define application-specific classes that can be passed around, particularly when using Services. Parcelables can be more sophisticated than Bundles, but this comes at a cost of significantly higher overhead.

Bundle and Parcel are both data serialization mechanisms, and for the most part both are used when application code is passing data across processes. However, because Parcel is much higher overhead that Bundle, Bundles are used in the more common places like the onCreate method, where overhead must be as low as possible. Parcels are most commonly used to allow applications to define Services with logical APIs that can use application-meaningful classes as method arguments and return values. If we required Bundle there, it would result in really clunky APIs. You should in general still keep your Service APIs as simple as possible, because primitives will serialize more efficiently than custom Parcelable classes.

关于android - 将位图放入包中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7727916/

相关文章:

android - 在 FirebaseUI 中更改 startActivityForResult 的布局

android - 我如何设计一个类似 "family tree"的用户界面?

java - 有没有办法通过锁定屏幕启动 Activity ?

c++ - 如何将这样的位图复制到DC?

php - 从 Symfony 4/5 中的另一个包覆盖包模板

android - 滚动ListView后,ListView View 底部的按钮收不到事件

java - map 位置更改监听器未触发

java - 将图片保存到变量中

iphone - ios bundle id 问题以启动第一个 facebook 应用程序

visual-studio-2015 - 在调试启动时自动重建 Javascript 包