Search code examples
javaandroidimageswitcher

Convert or Parse String to Uri and show image using ImageSwitcher


I would like to ask how to convert or parse below string and display it using ImageSwitcher on Android Studio. I tried parsing it with the Uri class parse Uri method but it did not work. Below is the code. int i=0;

public void getItemPhotos(String str ) {

    String[] arrOfStr = str.split(",", 3);
    for (String a : arrOfStr){
        Uri myUri = Uri.parse(a);
        images.add(myUri);
        i++;
    }

Tried showing it with this line below. viewSliderPhotos.setImageURI(images.get(i));

Sample of the string passed to the method above is the string below from firebase.

"https://firebasestorage.googleapis.com/v0/b/dnd-1ee0c.appspot.com/o/defaultpic.jpg?alt=media&token=224b6fdc-aae5-4dac-9576-53b3ab01262c"

Solution

  • It seems that you are trying to parse a string of URLs, separated by commas, and convert each URL into a Uri object, then display it using ImageSwitcher in Android Studio.

    One possible issue is that the URLs in the string you're passing into the getItemPhotos method may not be valid. You can try to log the URLs that you're trying to parse to make sure they're correct and that you have internet connection.

    If you are still having trouble displaying the image, you can try using the setFactory() method along with the setImageResource() method on the ImageSwitcher. This will make sure that the ImageSwitcher has a ViewFactory to create the views that you want to set the images on.

    viewSliderPhotos.setFactory(new ViewSwitcher.ViewFactory() {
                @Override
                public View makeView() {
                    ImageView myView = new ImageView(getApplicationContext());
                    myView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                    myView.setLayoutParams(new ImageSwitcher.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
                    return myView;
                }
            });
    viewSliderPhotos.setImageResource(R.drawable.image_name);

    If the problem still persists, please provide more context or error logs so that we can further investigate the issue.