0

I have an Activity which is used as an image gallery and works quite well. But it loads images from integer array which has references from drawables. And what I want to do is getting images from internet and store them in internal memory then use them in my gallery.

Here is my code below. Any help will be appreciated.

import okan.apps.tabbar.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;

 public class Activities extends Activity implements ViewFactory {

ImageSwitcher imageSwitcher ;

Integer[] imageList = {
        R.drawable.gallery,
        R.drawable.menu,
        R.drawable.promotion,
        R.drawable.info,
        R.drawable.activities       
};

int curIndex=0;
int downX,upX;

@Override
public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activities_main);

     imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
     imageSwitcher.setFactory(this);
     imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));
     imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));

     imageSwitcher.setImageResource(imageList[curIndex]);
     imageSwitcher.setOnTouchListener(new OnTouchListener() {
     public boolean onTouch(View v, MotionEvent event) {

         if (event.getAction() == MotionEvent.ACTION_DOWN) {
             downX = (int) event.getX(); 
             Log.i("event.getX()", " downX " + downX);
             return true;
         } 

         else if (event.getAction() == MotionEvent.ACTION_UP) {
             upX = (int) event.getX(); 
             Log.i("event.getX()", " upX " + downX);
             if (upX - downX > 100) {

                 //curIndex  current image index in array viewed by user
                 curIndex--;
                 if (curIndex < 0) {
                     curIndex = imageList.length-1;
                 }

                 imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(Activities.this,R.anim.slide_in_left));
                 imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(Activities.this,R.anim.slide_out_right));

                 imageSwitcher.setImageResource(imageList[curIndex]);
                 //GalleryActivity.this.setTitle(curIndex);
             } 

             else if (downX - upX > -100) {

                 curIndex++;
                 if (curIndex > 4) {
                     curIndex = 0;
                 }

                 imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(Activities.this,R.anim.slide_in_right));
                 imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(Activities.this,R.anim.slide_out_left));

                 imageSwitcher.setImageResource(imageList[curIndex]);
                 //GalleryActivity.this.setTitle(curIndex);
             }
                 return true;
             }
             return false;
         }
     });
}
public View makeView() {
    ImageView i = new ImageView(this);  
    i.setScaleType(ImageView.ScaleType.FIT_CENTER);
    return i;
}
}
osayilgan
  • 5,873
  • 7
  • 47
  • 68

1 Answers1

0

Use something like https://github.com/nostra13/Android-Universal-Image-Loader . Or you can check the code in https://github.com/cyrilmottier/GreenDroid . Personally I liked the first one since it was focused only on image loading.

Siyamed
  • 495
  • 2
  • 11
  • These are quite complex for the thing I want to do. Basicly, giving image pathes from internal memory instead of giving references to my interger array from drawable will be enough. I can handle rest of it. – osayilgan Apr 19 '12 at 21:59
  • so if I can understand you correctly you cannot refer to your images that are downloaded from internet as the images that are in your resources. therefore instead of using an int[] array you have to use a drawable or bitmap array. use setImageBitmap (or it was smth similar) to set the image of the imageview. – Siyamed Apr 19 '12 at 22:51
  • to convert a url content into image please either check those library codes or things like http://stackoverflow.com/questions/3090650/android-loading-an-image-from-the-web-with-asynctask. you have to download the image in a background thread and call the ui change (setImageBitmap) in the main thread. – Siyamed Apr 19 '12 at 22:55
  • Thanks for your response @Siyamed, I will try those and give a feed back as soon as possible for the solution I found. – osayilgan Apr 20 '12 at 06:16
  • The example that you suggested works with image view and I have ImageSwitcher. I couldnt find a way to work around of it. – osayilgan Apr 20 '12 at 08:49