Reputation: 45
my input is a parsed JSON containing a lot of pictures URLs, I created a list from those: "urlPictureList". I am trying to create an android view which contains 4 picture frames, each one of the frames supposed to show a picture taken from the urlPictureList, and on swipe change it to the next URL on the list. swiping in the opposite direction will change the picture back to the previous picture. (more than one swipe is possible and should show the previous picture in the list.
in my main activity, I used the following function in order to save the data:
public void sendMessage(View view) {
Intent intent = new Intent(this, act2 .class);
List<Song> songList = parseJson().second;
intent.putExtra("MainActivity", (Serializable) songList);
startActivity(intent);
}
and in my act2 class I found a function that supposed to download images from urls:
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
The download function doesn't work, and I am clueless about what to do next. my first problem is how to show the image on each of the frames. Secondly, the swipe gesture doesn't work for me, maybe I shouldn't use imageView?, I tried googling for it almost 1 week without success. thanks in advance
Upvotes: 0
Views: 161
Reputation: 3096
AsyncTasks are not ideal for most async solutions. There are better things available these days like Loaders and Services. For you I suggest you use a library for the loading of remote images because you need a lot more than this to ensure a smooth user experience like caching. Even if you solve this problem using a simple AsyncTask will give you a lot of trouble when your application state changes while downloading an image (AsyncTaskLoaders solve this).
All can be prevented by using a dedicated library for this like for example Picasso.
http://square.github.io/picasso/
If you still want to use the simple AsyncTask solution. Do you have code to start the AsyncTask?
new DownloadImageTask().execute(url);
For the swiping you can use a ViewPager
. Example
Upvotes: 1