与位图有关的Android parcelable问题

时间:2017-09-11 14:39:46

标签: android parcelable

由于某种原因偶尔将我的Vehicle对象作为parcelable传递给另一个活动时,它会超出最大大小:

  

java.lang.RuntimeException:android.os.TransactionTooLargeException:   数据包大小为569848字节

但奇怪的是,每次都不会发生这种情况。在我的应用程序中,我有一个图像,当我点击它然后它将它传递给另一个活动。多次单击此相同图像后,最终会导致该异常。我也在压缩Bitmap。有谁知道这个问题?

我还应该注意,如果我用30压缩,我也有同样的问题

package com.example.daniel.carbudgy.misc;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Parcel;
import android.os.Parcelable;

import com.example.daniel.carbudgy.R;
import com.example.daniel.carbudgy.tasks.ImageDownloaderCallback;
import com.example.daniel.carbudgy.tasks.ImageDownloaderTask;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Serializable;
import java.net.URL;

/**
 * Created by daniel on 11/09/17.
 */

public class Vehicle implements Parcelable {
    public interface VehicleHandler {
        public void ready();
    }

    private int id;
    private String name;
    private String colour;
    private String imageSrc;
    private Drawable image;
    private VehicleHandler handler;

    public Vehicle(Parcel in) {
        this.id = in.readInt();
        this.name = in.readString();
        this.colour = in.readString();
        this.imageSrc = in.readString();

        int length = in.readInt();
        byte[] buf = new byte[length];
        in.readByteArray(buf);

        // Convert Bitmap to Drawable:
        image = new BitmapDrawable(BitmapFactory.decodeByteArray(buf, 0, length));
    }
    public Vehicle(JSONObject vehicle_json) throws JSONException {
        JSONObject manufacture = vehicle_json.getJSONObject("manufactureModel").getJSONObject("manufacture");
        JSONObject model = vehicle_json.getJSONObject("manufactureModel").getJSONObject("model");
        String vehicle_name = manufacture.getString("manufacture") + " " + model.getString("model");
        Integer vehicle_id = vehicle_json.getInt("vehicleId");
        String image_src = vehicle_json.getJSONObject("imageUpload").getString("url");
        String colour = vehicle_json.getJSONObject("colour").getString("colour");
        setId(vehicle_id);
        setName(vehicle_name);
        setColour(colour);
        setImageSrc(image_src);
        image = null;
        handler = null;
    }

    public static final Creator<Vehicle> CREATOR = new Creator<Vehicle>() {
        @Override
        public Vehicle createFromParcel(Parcel in) {
            return new Vehicle(in);
        }

        @Override
        public Vehicle[] newArray(int size) {
            return new Vehicle[size];
        }
    };

    public void setHandler(VehicleHandler handler) {
        this.handler = handler;
    }

    public void load() {
        try {
            URL url = new URL(imageSrc);
            new ImageDownloaderTask(new ImageDownloaderCallback() {
                @Override
                public void success(Drawable[] drawables) {
                    image = drawables[0];
                    handler.ready();
                }

                @Override
                public void fail() {

                }
            }).execute(url);
        } catch(Exception ex) {
            throw new RuntimeException(ex);
        }
    }
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getColour() {
        return colour;
    }

    public void setColour(String colour) {
        this.colour = colour;
    }

    public String getImageSrc() {
        return imageSrc;
    }

    public void setImageSrc(String imageSrc) {
        this.imageSrc = imageSrc;
    }

    public Drawable getImage() {
        return image;
    }

    public void setImage(Drawable image) {
        this.image = image;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeInt(getId());
        parcel.writeString(getName());
        parcel.writeString(getColour());
        parcel.writeString(getImageSrc());

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ((BitmapDrawable)getImage()).getBitmap().compress(Bitmap.CompressFormat.PNG, 0, out);
        parcel.writeInt(out.size());
        parcel.writeByteArray(out.toByteArray());
    }


}

1 个答案:

答案 0 :(得分:3)

  

有谁知道这个问题?

不要在保存的实例状态Bitmap中传递IntentBundle个附加内容,而是传递允许另一方使用位图的信息:

  • 从流程中的某个位图缓存中提取

  • 再次从原始来源加载位图(例如,https网址)

基于Binder的IPC交易有1MB的限制。该限制在所有未完成的交易上,因此多个同时交易的组合内存使用量需要低于1MB。因此,您看到的差异可能来自:

  • 根据分辨率改变Bitmap本身的内存占用量,或

  • 改变当时正在进行的其他IPC交易

您的目标是确保任何此类交易 - 例如,在您的流程之外传递Intent - 应该远远超过1MB的限制,因此即使有一些交易同时发生,合计总数不会达到1MB。