1

In my activity i'm getting list of images that should be display in a slideshow,

now Source for list of images that should display comes from JSON so I have parse json and got list of images that i need to show from Appfolder/app_content

I have Class:

MainActivity:

  public class MainActivity extends Activity {

        ArrayList<String> imageSlideList = new ArrayList<String>();

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

             //Get Intent- Extra
        Intent intent = getIntent();
            String swpImgSImgs = intent.getStringExtra("swpImgS");

         try {
             JSONArray array = new JSONArray(swpImgSImgs);
             for (int i = 0 ; i< array.length(); i++){
                String imageList = array.getString(i);
                FileOperation fo= new FileOperation();
                File   dir=fo.createFileManagerInsidePackage(AppContext.getAppContext());
                String imagePath = dir+"/"+imageList;
                imageSlideList.add(imagePath);
             }
         } catch (JSONException e) {
             e.printStackTrace();
         }  
            ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
            ImageAdapter adapter = new ImageAdapter(this);
            viewPager.setAdapter(adapter);


        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            return false;
        }

    }

Adapter:

    public class SwipingImageSlidesAdapter extends PagerAdapter {



        Context context;

//I need to use files that are in json 
        private int[] GalImages = new int[] {
            R.drawable.camera,
            R.drawable.camera
        };

        SwipingImageSlidesAdapter(Context context){
            this.context=context;
        }
        @Override
        public int getCount() {
            return GalImages.length;
        }

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

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView imageView = new ImageView(context);
            int padding = context.getResources().getDimensionPixelSize(R.dimen.activity_horizontal_margin);
            imageView.setPadding(padding, padding, padding, padding);
            imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            imageView.setImageResource(GalImages[position]);
            ((ViewPager) container).addView(imageView, 0);
            return imageView;
        }

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

Now what changes do i have to make to display images from Application-folder(App/app_content) instead of using R.id.imageName using that ArrayList that I created imageSlideList in MainActivity?

Krrishnaaaa
  • 700
  • 11
  • 23
DPP
  • 12,146
  • 2
  • 46
  • 45

3 Answers3

1

you can get the image from aarraylist like way

imageView.setImageResource((int)aimageSlideList.get(position));

pass the arraylist in adapter class

private ArrayList<String> aimageSlideList=null;
private LayoutInflater mInflater=null;


    public SwipingImageSlidesAdapter (Activity context,ArrayList<String> aimageSlideList) {
        super(context, 0);

        mInflater = context.getLayoutInflater();
        this.aimageSlideList=aimageSlideList;
        }
Sunil Kumar
  • 7,083
  • 4
  • 31
  • 50
1

Pass the list of path to the constructor of the adapter.

     ImageAdapter adapter = new ImageAdapter(this,imageSlideList)

Then in Adapter

      ArrayList<String> mList;
      SwipingImageSlidesAdapter(Context context,ArrayList<String> list){
        this.context=context;
        mList = list; 
    } 

Then in getView

        Bitmap b = BitmapFactory.decodeFile(mlist.get(position));
        imageView.setImageBitmap(b);

public static Bitmap decodeFile (String pathName)

Decode a file path into a bitmap. If the specified file name is null, or cannot be decoded into a bitmap, the function returns null.

Parameters

pathName complete path name for the file to be decoded.

Returns

the resulting decoded bitmap, or null if it could not be decoded.

Raghunandan
  • 131,557
  • 25
  • 223
  • 252
0
  1. Just create new Class with getter Setter Of Bitmap/Drawable.

  2. Now set your image Bitmap/Drawable in MainActivity.

  3. In your Adapter class, get that image by position.

Bhoomika Brahmbhatt
  • 7,226
  • 3
  • 26
  • 44