2

I have a GridView populated with images. When I click on a GridView item it should open a new Activity and show an image in full screen, but it doesn't happen.

I'm using the Glide library. I've tried to debug, but I didn't find the problem.

Any ideas?

Gallery.java

  gridViewGallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {


                Intent intent = new Intent(AdvertisementGallery.this,FullScreenImage.class);
                intent.putExtra("image",i);

                startActivity(intent);

            }
        });

FullScreenImage.java

 viewPager.clearFocus();


        imageItems = Constants.items;


        bitmap = getIntent().getExtras().getInt("image");


        //   ImagePageAdapter imagePageAdapter = new ImagePageAdapter(this,imageItems);
        imagePageAdapter = new ImageAdapter(this,imageItems);
        viewPager.setAdapter(imagePageAdapter);

        viewPager.setCurrentItem(bitmap,false);

class ImageAdapter extends PagerAdapter {

    public Context context;
    public ArrayList data = new ArrayList();

    public ImageAdapter(Context context, ArrayList data){

        this.context = context;
        this.data = data;

    }
    @Override
    public int getCount() {
        return this.data.size();

    }

    @Override
    public int getItemPosition(Object object) {
        int position = data.indexOf(object);
        return  POSITION_NONE ;

    }

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

    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View row = null;


        if (row == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(R.layout.image_pager_item_list_layout, container, false);

            //  holder.imageTitle = (TextView) row.findViewById(R.id.text);
            image = (TouchImageView) row.findViewById(R.id.imgDisplay);
            row.setTag(image);
        } else {
            image = (ImageView) row.getTag();
        }

        ImageItem item = (ImageItem) data.get(position);

       Glide.with(getApplicationContext())
                .load(item.getImage())
               .into(image);

        container.addView(row);

        return row;

    }

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


    }
}
Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
rahul
  • 87
  • 2
  • 8

2 Answers2

1

First of all, you are passing the position of the image. you need to pass the item. instead of position. you need to do following changes in the following way.

in Gallery.java

 gridViewGallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            ImageItem item = (ImageItem) data.get(position)

            Intent intent = new Intent(AdvertisementGallery.this,FullScreenImage.class);
            //intent.putExtra("image",i);
            intent.putExtra("imagePosition",i);

            startActivity(intent);

        }
    });

Now, Retrieve your image in FullScreenImage.java by following way.

imageItems = Constants.items;

int position=getIntent().getExtras().getInt("imagePosition");
ImageItem item = imageItems.get(position);
bitmap = item.getImage();

To set in your ViewPager you can follow this code:

imagePageAdapter = new ImageAdapter(this,imageItems);
viewPager.setAdapter(imagePageAdapter);
viewPager.setCurrentItem(position, true);

And for the issue of

some time image display and some time is not display in viewpager.

Try to add following line .placeholder (new ColorDrawable (Color.WHITE)). so the code looks like :

Glide.with(getApplicationContext())
            .load(item.getImage())
            .placeholder (new ColorDrawable (Color.WHITE))
           .into(image);

And, If you are getting issue only in release mode apk then try this solution

Riddhi Shah
  • 3,092
  • 5
  • 36
  • 56
  • @rahul Can you show me the code of class ImageItem? – Riddhi Shah Aug 30 '18 at 05:15
  • i was tried it but image is not show. i using glide library is not show image in viewpager. when i used universal image loader library its show image in view pager. so, how to image not show through glide library. – rahul Aug 30 '18 at 11:23
  • @rahul If you want to load your image as a bitmap, you need to call `asBitmap()` method. `Glide.with(getApplicationContext()) .load(item.getImage()).asBitmap() .into(bitmap);` where bitmap is bitmap of your image. – Riddhi Shah Aug 30 '18 at 11:40
  • i load image from server url – rahul Aug 30 '18 at 12:11
  • @rahul then no need to use `asBitmap` it should load automatically. make sure you have an active internet connection and the URL from which you are loading is proper working. And in none of the above cases, Please check your logcat may be you will find the cause of the problem there. – Riddhi Shah Aug 30 '18 at 12:31
  • @rahul did you got your solution? – Riddhi Shah Aug 31 '18 at 13:18
  • some time image display and some time is not display in viewpager. – rahul Sep 01 '18 at 06:17
  • image not show in viewpager is problem solved but above problem is occurred.what i do problem is solved. give, any suggestion. i using Glide library for image load. – rahul Sep 01 '18 at 06:22
  • @rahul have you checked your logcat? did you find anything there? or you can refer this link if you want https://stackoverflow.com/a/39336438/3342570 or https://github.com/bumptech/glide/issues/1908 – Riddhi Shah Sep 04 '18 at 05:35
  • nothing show in logcat. some image load in two time gridview tap after load in viewpager.its some image loading issue – rahul Sep 04 '18 at 06:01
  • @rahul is it issue for specific images only? have you checked the URL you are trying to load is working? have you checked internet connection? – Riddhi Shah Sep 04 '18 at 06:29
  • in gridview loading perfectly but viewpager load some issue – rahul Sep 04 '18 at 06:44
  • @rahul have you tried solutions in the link i mentioned in above comments – Riddhi Shah Sep 04 '18 at 06:48
  • stackoverflow ya github – rahul Sep 04 '18 at 07:00
  • @rahul try both one by one. and did you try googling your problem? If not do it now. and still, you did not find any solution then feel free to tell I also will help to try to find out the solution. but, first, you have to search for it on the google – Riddhi Shah Sep 04 '18 at 07:13
  • i have googling my problem after i post on stackoverflow – rahul Sep 04 '18 at 07:15
  • @rahul try these links https://github.com/bumptech/glide/issues/2184 or https://github.com/bumptech/glide/issues/935 – Riddhi Shah Sep 05 '18 at 05:14
  • @rahul Did these links were helpful? and feeling pleasure to help you – Riddhi Shah Sep 05 '18 at 06:46
  • i have add placeholder in glide image load issue solved but in a released apk placeholder image is not display – rahul Sep 05 '18 at 08:32
  • is placeholder image is url ? – Riddhi Shah Sep 05 '18 at 09:18
  • placeholder(R.drawable.logo) – rahul Sep 05 '18 at 09:29
  • release apk publish on playstore after is not show. – rahul Sep 05 '18 at 09:30
  • @rahul do the steps mentioned in my previous post. maybe it will help you https://stackoverflow.com/a/49934146/3342570 – Riddhi Shah Sep 05 '18 at 09:33
  • @rahul if you find my answer as a helpful one and if it has solved your problem. then please accept my answer. – Riddhi Shah Sep 05 '18 at 13:30
  • don't any response. – rahul Sep 06 '18 at 08:28
  • @rahul response of what and by whom? – Riddhi Shah Sep 06 '18 at 08:30
0

Check how to send an image from one activity to another activity via intent. In your code,what you are sending is not an image that is a position. How did you get the position in the bitmap? this is not the way. sorry for my english.

check this link

first, convert the image into bitmap then pass bitmap to other activity and receive it.

Riddhi Shah
  • 3,092
  • 5
  • 36
  • 56
Anil Atri
  • 68
  • 8