0
votes

I am trying to view a couple of images in my App and the image links are saved in Firebase realtime database. I uploaded these links as a .JSON and did not manually add them to the realtime database. When attempting to retrieve these images in my App, It crashes after a few seconds of clicking the button to launch the images ("ZamCatalog has stopped"). In the logs, I am getting the error below. Am not sure where am going wrong, please help.

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.android.zamcatalog, PID: 13529 com.google.firebase.database.DatabaseException: Failed to convert value of type java.util.ArrayList to String at com.google.android.gms.internal.zzelw.zzb(Unknown Source:93) at com.google.android.gms.internal.zzelw.zza(Unknown Source:0) at com.google.firebase.database.DataSnapshot.getValue(Unknown Source:10) at com.example.android.zamcatalog.gameActivity$1.onDataChange(gameActivity.java:43) at com.google.android.gms.internal.zzegf.zza(Unknown Source:13) at com.google.android.gms.internal.zzeia.zzbyc(Unknown Source:2) at com.google.android.gms.internal.zzeig.run(Unknown Source:71) at android.os.Handler.handleCallback(Handler.java:789) at android.os.Handler.dispatchMessage(Handler.java:98) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6944) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374) Application terminated.

Below is a snapshot of the images saved in the database

Firebase Realtime Database

gameActivity code to retrieve images

public class gameActivity extends AppCompatActivity {

ViewPager viewPager;
ViewPagerAdapter adapter;

private FirebaseDatabase firebaseDatabase=FirebaseDatabase.getInstance();
private DatabaseReference reference=firebaseDatabase.getReference();
//private DatabaseReference childreference=reference.child("images");
private DatabaseReference gameReference=reference.child("game");

private ArrayList<String> images = new ArrayList<String>();

@Override
protected void onStart() {
    super.onStart();
    gameReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String images = dataSnapshot.getValue(String.class);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
           // Log.w(TAG, "loadImage:onCancelled", databaseError.toException());

        }
    });
}

    viewPager = (ViewPager)findViewById(R.id.viewpager);
    adapter = new ViewPagerAdapter(gameActivity.this,images);
    viewPager.setAdapter(adapter);
}

ViewPager Adapter

public class ViewPagerAdapter extends PagerAdapter {
Activity activity;
ArrayList<String> images;
LayoutInflater inflater;

public ViewPagerAdapter(Activity activity, ArrayList<String> images) {
    this.activity = activity;
    this.images = images;
}

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

@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
    return view == object;
}

@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
    inflater = (LayoutInflater) activity.getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View itemview = inflater.inflate(R.layout.viewpager_item, container, false);

    ImageView image;
    image = (ImageView) itemview.findViewById(R.id.imageView);
    DisplayMetrics dis = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(dis);
    int height = dis.heightPixels;
    int width = dis.widthPixels;
    image.setMinimumHeight(height);
    image.setMinimumWidth(width);

    try {
        Glide.with(activity.getApplicationContext())
                //.using(new FirebaseImageLoader())
                .load(images.get(position))  //This is were you put the storageReference
                .into(image);
    } catch (Exception ex) {

    }

    //notifyDataSetChanged();
    container.addView(itemview);
    return itemview;

}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    ((ViewPager) container).removeView((View) object);
}

}

1
The Firebase SDK considers the child elements under game to be a List of Strings, but you're asking for a String when calling getValue(). Treat it as a List instead. - Doug Stevenson
can you say how you did you add the image url to database - Jins Lukose

1 Answers

0
votes

To correctly get all those urls, please use the following lines of code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference gameRef = rootRef.child("game");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String url = ds.getValue(String.class);
            Log.d("TAG", url);
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage());
    }
};
gameRef.addListenerForSingleValueEvent(valueEventListener);

The output in your logcat will be all those urls.