0

I'm sure this is gonna be a doozy but keep in mind I'm brand new to Android development so if you can't give me complete code examples please ignore this question :)

I have imageview1 and in it I need to display a random image when the app loads. The images will be stored on an FTP server in a password protected folder.

The images will be consecutively named 1.jpg, 2.jpg, 3.jpg etc.

So I need to first open the location and get a count of how many images are in there. Then I need to choose one at random. Then I need to display that image in my imageview.

Thank for any help or pointing me in the right direction.

Jimmy D
  • 5,282
  • 16
  • 54
  • 70
  • 2
    Rule of StackOverflow: you need to ask specific questions: http://stackoverflow.com/faq – Peter Knego May 04 '11 at 18:45
  • 1
    Dividing the original problem in a series of questions might lead first one to relate in opening FTP connection; http://stackoverflow.com/questions/1567601/android-ftp-library – harism May 04 '11 at 18:50

2 Answers2

1

Use the apache commons ftp library (StackOverflow discussion here). Use the listNames() method, parse the file names to see how many match your criteria (or just assume that every named file in that directory is applicable) and use java.util.Random.nextInt (or your favorite random number generator) to decide which one to download.

Community
  • 1
  • 1
Alec B. Plumb
  • 2,490
  • 25
  • 25
0

Android does not, AFAIK, support FTP listing - only downloading from a known URL. So you need to find another way to pass the number of files - say, a small text file alongside the pictures, with an integer in it.

For FTP download guidance, read up on class URLConnection. it supports nonanonymous FTP.

Once it's downloading, parse it into a BitmapDrawable. You can pass the input stream straight to the BitmapDrawable constructor.

Then pass the Drawable to the imageview.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
  • I like the text file approach. So let's say I have a text file with an integer in it, say 134. I need to read that number and then use it to initiate a random number generator (between 1 and 134). Any thoughts? Or should I break this up into 2 questions? The text file could reside at http://www.myserver.com/input.txt – Jimmy D May 04 '11 at 19:15
  • Google for "android random number generator". See how easy? – Seva Alekseyev May 04 '11 at 19:24
  • Seva - If we all did that this forum wouldn't have a reason to exist now would it? See how that works? – Jimmy D May 04 '11 at 19:38
  • This is not a forum. And trust me, SO is full of questions that *don't* have an answer one straightforward Google search away. – Seva Alekseyev May 04 '11 at 19:42
  • By the way, Android's RNG is in `java.util.Random`. – Seva Alekseyev May 04 '11 at 19:49