Question

I have made a random Image Gallery view with the Android Touch Gallery but I want to Show random Images. I have tried to generate an link with an random number.

I can not Play it and I have no idea how i can solve this.

Please Help.

Activity:

package com.ddd.fun1234;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import ru.truba.touchgallery.GalleryWidget.GalleryViewPager;
import ru.truba.touchgallery.GalleryWidget.UrlPagerAdapter;
import ru.truba.touchgallery.GalleryWidget.BasePagerAdapter.OnItemChangeListener;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;

public class GalleryUrlAvtivity extends Activity {

private GalleryViewPager mViewPager;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

public Bitmap GetImage() {
    Random rn = new Random();
    int n = 200000 - 199000 + 1;
    int i = rn.nextInt() % n;    

    URL tempURL = null;
    try {
        tempURL = new URL("http://miniz.co/RageToonApp/Images/" + rn + ".jpg");
    } catch (MalformedURLException e1) {
        e1.printStackTrace();
    }

    List<String> items = new ArrayList<String>();
    Collections.addAll(items, tempURL);

    UrlPagerAdapter pagerAdapter = new UrlPagerAdapter(this, items);
    pagerAdapter.setOnItemChangeListener(new OnItemChangeListener()
    {
        @Override
        public void onItemChange(int currentPosition)
        {

        }
    });

    mViewPager = (GalleryViewPager)findViewById(R.id.viewer);
    mViewPager.setOffscreenPageLimit(3);
    mViewPager.setAdapter(pagerAdapter);
}

}

There is a fail in Collections.addAll. What can I use instead of this?

When you have an idea what i can do or what is a good second Option, please write it.

Daniel

Was it helpful?

Solution

The problem is in these lines:

Random rn = new Random();
...
int i = rn.nextInt() % n;    
...
tempURL = new URL("http://miniz.co/RageToonApp/Images/" + rn + ".jpg");

You are calculating i but instead of using it in the URL, you're using rn instead. By default, this calls Random.toString() which contains characters (in this case, a @) that are illegal in URLs.

To fix this you should change the last line to:

tempURL = new URL("http://miniz.co/RageToonApp/Images/" + i + ".jpg");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top