Using SetImageUri in ListView Adapter not working

So I'm playing around with a few concepts for an application, I have an activity where the user can select an image from the gallery as part of an object and save that to sqlite, the uristring is saved in a field. When the URI is applied to the ImageView on that layout as part of the ActivityResult, the image shows as expected.

On the page that is a list of the saved objects, the ImageView isn't displaying the image after the Uri has been set in the GetView method below.

Not sure what I'm missing or if I should approach this differently. The URI that I generate from the URI String is the same as the one returned from the gallery selection so I can only assume it's some odd workflow/life cycle issue.

Any advice appreciated! Cheers.

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        View view = convertView; // re-use an existing view, if one is available
        if (view == null) // otherwise create a new one
            view = context.LayoutInflater.Inflate(Resource.Layout.CarRowView, null);

        view.FindViewById<TextView>(Resource.Id.Text1).Text = String.Format("{0} {1}", items[position].Make, items[position].Model);
        view.FindViewById<TextView>(Resource.Id.Text2).Text = "some text";
        var imageView = view.FindViewById<ImageView>(Resource.Id.Image);

        Android.Net.Uri uri = Android.Net.Uri.Parse(items[position].ImageUri);
        imageView.SetImageURI(uri);
        return view;
    }

Answers

  • IeuanMeredithIeuanMeredith USMember
    edited January 2016

    Have just found that this is working when I run on KitKat, but doesn't work on Marshmallow, if that helps narrow things down.

    EDIT: Nevermind, it now works in Marshmallow too. Amazing what a PC restart will do sometimes...

  • IeuanMeredithIeuanMeredith USMember
    edited January 2016

    Further update, as I'm storing the Image Uri string, I've needed to implement the following additions so that the images can be loaded on app start.

                var intent = new Intent();
                if(Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Kitkat){
                    intent = new Intent(Intent.ActionOpenDocument);
                    intent.PutExtra(Intent.ExtraAllowMultiple, true);
                    intent.AddFlags(ActivityFlags.GrantPersistableUriPermission);
                }else{
                    intent = new Intent(Intent.ActionGetContent);
                }
                intent.PutExtra(Intent.ExtraLocalOnly, true);
                intent.AddFlags(ActivityFlags.GrantReadUriPermission);
                intent.SetType("image/*");
                StartActivityForResult(
                    Intent.CreateChooser(intent, "Select photo"), 0);</del>
    

    Further revision, I'm now storing the image away from the gallery and storing the filepath instead of trying to reference it from the gallery each time the app loads.

                var appFolderPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
                var filePath = System.IO.Path.Combine(appFolderPath, "filename.png");
                var stream = new FileStream(filePath, FileMode.Create);
                bmp.Compress(Bitmap.CompressFormat.Png, 100, stream);
                stream.Close();
    
Sign In or Register to comment.