3

I'm developing a Media Player Application where I'm using ArrayList to store the list of songs, and want to use that same list between a Service and other Activities. I've written a custom type Songs implementing Parcelable Interface. This is how I did it:

     String ID, Title, Artist, Album, Genre, Duration, Path;
        byte[] AlbumArt;

    //constructors go here
        //getters and setters go here    

public Songs(Parcel in) {
        readFromParcel(in);
    }
            @Override
                    public void writeToParcel(Parcel dest, int flags) {
                        // TODO Auto-generated method stub
                        dest.writeString(this.ID);
                        dest.writeString(this.Title);
                        dest.writeString(this.Artist);
                        dest.writeString(this.Album);
                        dest.writeString(this.Genre);
                        dest.writeString(this.Duration);
                        dest.writeByteArray(this.AlbumArt);
                        dest.writeString(this.Path);
                    }

                    private void readFromParcel(Parcel in) {
                        this.ID = in.readString();
                        this.Title = in.readString();
                        this.Artist = in.readString();
                        this.Album = in.readString();
                        this.Genre = in.readString();
                        this.Duration = in.readString();
                        in.readByteArray(this.AlbumArt);
                        this.Path = in.readString();
                    }

                    public static final Parcelable.Creator<Songs> CREATOR = new Parcelable.Creator<Songs>() {

                        @Override
                        public Songs createFromParcel(Parcel source) {
                            // TODO Auto-generated method stub
                            return new Songs(source); // using parcelable constructor
                        }

                        @Override
                        public Songs[] newArray(int size) {
                            // TODO Auto-generated method stub
                            return new Songs[size];
                        }
                    };

Now the Problem is I'm getting FAILED BINDER TRANSACTION when I try to pass the Arraylist<Songs> in an Intent. As a workaround I'm using static variables. Any ideas as on how to overcome this solution and pass the ArrayList<Songs> in an Intent.

PsyGik
  • 3,535
  • 1
  • 27
  • 44
  • In this case I have used Global static variables and the problems gone. You can get idea from [this SO Post](https://stackoverflow.com/a/13226397/7608371). – Noor Hossain Jun 01 '22 at 10:18

1 Answers1

3

Given byte[] AlbumArt, you are probably exceeding the 1MB IPC limit.

Any ideas as on how to overcome this solution and pass the ArrayList in an Intent.

I'd start by getting rid of AlbumArt. Images should be in an image cache, one designed to make sure that you do not run out of heap space by having a maximum size and getting rid of least-recently-used entries (or moving them to a second-tier disk cache). As a side benefit, moving the byte[] out of the Parcelable will probably clear up your problem.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Okay. I'll do that and check.I've come across an ImageLoader library at which I'll take a look. But in general,is my method correct to make that object (with byte[]) Parcelable? – PsyGik Feb 18 '14 at 11:55
  • @PsyGik: "is my method correct to make that object (with byte[]) Parcelable?" -- if by "correct" you mean "will it run, if your `ArrayList` is small enough to not hit the 1MB limit", it looks OK. Personally, I am not a big fan of passing whole models around internally this way. IMHO, your models themselves should be a cache, managed by a singleton, and you pass around some IDs or keys or something in `Intent` extras. This saves a chunk of memory and CPU time, as you're not copying and re-copying and re-re-copying and re-re-re-copying and re-re-re-re-copying the same model objects. – CommonsWare Feb 18 '14 at 11:59
  • need help on the somewhat similar issue: http://stackoverflow.com/q/42062417/6144372 – Hammad Nasir Feb 06 '17 at 10:30