Using the default android image Viewer to show pic

2019-07-22 16:14发布

问题:

I know that I can open an image using the default android image Viewer, for example with new Intent(Intent.ACTION_VIEW) etc.

If I open the image and then I swipe left/right I'll see the other images saved on the device.

For example, if I open one of whatsapp images, swiping left/right I'll see all other images saved inside whatsapp folder.

Is there a way to pass the default android image Viewer a List/Array of uri, in order to prevent users from swiping left/right and see all the images on the device?

I want the user to swipe left/right and see only images I allow him to see.

Thank you in advance

回答1:

try this you can use android.support.v4.view.ViewPager

Layout manager that allows the user to flip left and right through pages of data. You supply an implementation of a PagerAdapter to generate the pages that the view shows.

<android.support.v4.view.ViewPager
    android:id="@+id/viewPager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

</android.support.v4.view.ViewPager>

demo code

ViewPager viewPager;
ArrayList<String> imageArray;
imageArray = new ArrayList<>();
viewPager = findViewById(R.id.cspl_viewPager);
imageArray.add(R.drawable.bg);
imageArray.add(R.drawable.bg);
imageArray.add(R.drawable.bg);
imageArray.add(R.drawable.bg);
imageArray.add(R.drawable.bg);
imageArray.add(R.drawable.bg);
ImageAdapter adapter = new ImageAdapter(this, imageArray);
viewPager.setAdapter(adapter);

now create ImageAdapter like this

    public class ImageAdapter extends PagerAdapter {

        Context context;
        ArrayList<String> imageArray;

        public ImageAdapter(Context context, ArrayList<String> imageArray) {
            this.context = context;
            this.imageArray = imageArray;
        }

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

        @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);

            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, 50);
            imageView.setLayoutParams(layoutParams);

            int padding = context.getResources().getDimensionPixelSize(R.dimen.font_size_10);
            imageView.setPadding(padding, padding, padding, padding);

//            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

            Glide.with(ProfileActivity.this)
                    .load(R.drawable.bg)
                    .into(imageView);


            ((ViewPager) container).addView(imageView, 0);

            return imageView;

        }

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


回答2:

Best scenario is you can use viewpager with your image array or image list and you can get the functionality of swipe left or right, Nothing you need to do more just implement viewpager and create one adapter to fetch image when getItem event called,