Convert a string which contains uri paths to a arraylist

I am creating a notepad programm where the user can add images to his notes . When he creates a new note and picks a few images their paths(uri) are stored in an arraylist which is converted to a xml tag (string type conversion) . When the user wants to open a created note the selected images from before are displayed in a gridview. However when i try to convert the string tag from the xml file to an arraylist so the imageadapter can put them in the grid view i am getting errors about conversion types . edit the error i get is -java.lang.String cannot be cast to android.net.Uri-

The adapter is this

`private Context ctx;
    private int pos;
    private LayoutInflater inflater;
    private ImageView ivGallery;
    ArrayList<Uri> mArrayUri;
    public GalleryAdapter(Context ctx, ArrayList<Uri> mArrayUri) {


        this.ctx = ctx;
        this.mArrayUri = mArrayUri;
    }

    @Override
    public int getCount() {
        return mArrayUri.size();
    }

    @Override
    public Object getItem(int position) {
        return mArrayUri.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        pos = position;
        inflater = (LayoutInflater) ctx
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View itemView = inflater.inflate(R.layout.gv_item, parent, false);

        ivGallery = (ImageView) itemView.findViewById(R.id.ivGallery);

        ivGallery.setImageURI(mArrayUri.get(position));

        return itemView;
    }`

and the way i try to convert them to array and show them in the grid view is this

`  String Paths = userData.get(1);


            ArrayList mArrayUri = new ArrayList<>(Arrays.asList(Paths.split(",")));
            galleryAdapter = new GalleryAdapter(getApplicationContext(),mArrayUri);
            gvGallery.setAdapter(galleryAdapter);
            gvGallery.setVerticalSpacing(gvGallery.getHorizontalSpacing());
            ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) gvGallery
                    .getLayoutParams();
            mlp.setMargins(0, gvGallery.getHorizontalSpacing(), 0, 0);

`

The xml creation code is this

` xmlSerializer.startDocument("UTF-8", true);
            xmlSerializer.startTag(null, "userData");
            xmlSerializer.startTag(null,"Text");
            xmlSerializer.text(NoteText);
            xmlSerializer.endTag(null, "Text");
            xmlSerializer.startTag(null,"Image Paths");
            xmlSerializer.text(String.valueOf(Gallery.ImagePaths));
            xmlSerializer.endTag(null,"Image Paths" );
            xmlSerializer.endTag(null, "userData");
            xmlSerializer.endDocument();`

Answer

Your code is pretty good just make your array list type string and then take string value of uri.

In the .text(String.valueof(Imagepath)) use Uri.toString instead of String valueof

Example :

Uri uri;
String myUriString;
myUriString = uri.toString();

Leave a Reply

Your email address will not be published. Required fields are marked *