How to pass an array of Uri between Activity using Bundle

后端 未结 4 1742
情话喂你
情话喂你 2021-01-13 01:43

I need to pass an array of Uri to another activity, to pass an array of String I use simply

String[] images=getImagesPathString();

    Bundle b = new Bundle         


        
4条回答
  •  南笙
    南笙 (楼主)
    2021-01-13 02:19

    You should look into the Parcelable interface to see how to pass things on an intent

    http://developer.android.com/intl/es/reference/android/os/Parcelable.html

    Maybe you can implement a ParcelableUri class that implements that interface.

    Like this (not tested!!):

    public class ParcelableUri implements Parcelable {
    
    private Uri[] uris;
    
    public ParcelableUri(Parcel in) {
        Uri.Builder builder = new Uri.Builder();
    
        int lenght = in.readInt();
        for(int i=0; i<=lenght; i++){           
            uris[i]= builder.path(in.readString()).build();
        }
    }
    
    @Override
    public int describeContents() {
        return 0;
    }
    
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(uris.length);
        for(int i=0; i<=uris.length; i++){
            dest.writeString(uris[i].toString());
        }
    }
    
    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
    
        public ParcelableUri createFromParcel(Parcel in) {
            return new ParcelableUri(in);
        }
    
        public ParcelableUri[] newArray(int size) {
            return new ParcelableUri[size];
        }
    };
    

提交回复
热议问题