简体   繁体   English

从ViewPager中的ImageView将图像保存到图库

[英]Saving image to gallery from imageview in viewpager

I managed to save the image from imageview to gallery with the onlongclicklistner() with the help of code given below. 我设法通过onlongclicklistner()在下面给出的代码的帮助下将图像从imageview保存到图库。 But the problem is that it always save the last image dosent matters which image i try to save. 但是问题在于,它总是保存最后的图像,这与我尝试保存的图像很重要。

public class CapturePhotoUtils {


        public final String insertImage(ContentResolver cr,
                                               Bitmap source,
                                               String title,
                                               String description) {

            ContentValues values = new ContentValues();
            values.put(MediaStore.Images.Media.TITLE, title);
            values.put(MediaStore.Images.Media.DISPLAY_NAME, title);
            values.put(MediaStore.Images.Media.DESCRIPTION, description);
            values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
            // Add the date meta data to ensure the image is added at the front of the gallery
            values.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis());
            values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());

            Uri url = null;
            String stringUrl = null;    /* value to be returned */

            try {
                url = cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

                if (source != null) {
                    OutputStream imageOut = cr.openOutputStream(url);
                    try {
                        source.compress(Bitmap.CompressFormat.JPEG, 50, imageOut);
                    } finally {
                        imageOut.close();
                    }

                    long id = ContentUris.parseId(url);
                    // Wait until MINI_KIND thumbnail is generated.
                    Bitmap miniThumb = MediaStore.Images.Thumbnails.getThumbnail(cr, id, MediaStore.Images.Thumbnails.MINI_KIND, null);
                    // This is for backward compatibility.
                    storeThumbnail(cr, miniThumb, id, 50F, 50F, MediaStore.Images.Thumbnails.MICRO_KIND);
                } else {
                    cr.delete(url, null, null);
                    url = null;
                }
            } catch (Exception e) {
                if (url != null) {
                    cr.delete(url, null, null);
                    url = null;
                }
            }

            if (url != null) {
                stringUrl = url.toString();
            }

            return stringUrl;
        }


        private final Bitmap storeThumbnail(
                ContentResolver cr,
                Bitmap source,
                long id,
                float width,
                float height,
                int kind) {

            // create the matrix to scale it
            Matrix matrix = new Matrix();

            float scaleX = width / source.getWidth();
            float scaleY = height / source.getHeight();

            matrix.setScale(scaleX, scaleY);

            Bitmap thumb = Bitmap.createBitmap(source, 0, 0,
                    source.getWidth(),
                    source.getHeight(), matrix,
                    true
            );

            ContentValues values = new ContentValues(4);
            values.put(MediaStore.Images.Thumbnails.KIND,kind);
            values.put(MediaStore.Images.Thumbnails.IMAGE_ID,(int)id);
            values.put(MediaStore.Images.Thumbnails.HEIGHT,thumb.getHeight());
            values.put(MediaStore.Images.Thumbnails.WIDTH,thumb.getWidth());

            Uri url = cr.insert(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, values);

            try {
                OutputStream thumbOut = cr.openOutputStream(url);
                thumb.compress(Bitmap.CompressFormat.JPEG, 100, thumbOut);
                thumbOut.close();
                return thumb;
            } catch (FileNotFoundException ex) {
                return null;
            } catch (IOException ex) {
                return null;
            }
        }
    }

I am putting images from the viewpager getting images from array of drawables 我正在从viewpager放置图像以从可绘制数组中获取图像

class CustomPagerAdapter extends PagerAdapter { CustomPagerAdapter类扩展了PagerAdapter {

    Context mContext;
    LayoutInflater mLayoutInflater;

    public CustomPagerAdapter(Context context) {
        mContext = context;
        mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return mResources.length;
    }


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

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View itemView = mLayoutInflater.inflate(R.layout.image_slider_item, container, false);

        imageView = (TouchImageView) itemView.findViewById(R.id.imageView);
        imageView.setImageResource(mResources[position]);

        imageView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {

                CapturePhotoUtils photoUtils = new CapturePhotoUtils();
                imageView.setDrawingCacheEnabled(true);
                Bitmap b = imageView.getDrawingCache();
                photoUtils.insertImage(Full_Screen_Slider.this.getContentResolver(),
                    b, "1image", "this is downloaded image sample");
                Toast.makeText(mContext, "longpress ", Toast.LENGTH_SHORT).show();

                return true;
            }
        });

        container.addView(itemView);

        return itemView;
    }

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

}

Replace this 取代这个

@Override
    public Object instantiateItem(ViewGroup container,final int position) {
       final View itemView = mLayoutInflater.inflate(R.layout.image_slider_item, container, false);

      final TouchImageView imageView = (TouchImageView) itemView.findViewById(R.id.imageView);
        imageView.setImageResource(mResources[position]);

        imageView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {

                CapturePhotoUtils photoUtils = new CapturePhotoUtils();
                imageView.setDrawingCacheEnabled(true);
                Bitmap b = imageView.getDrawingCache();
                photoUtils.insertImage(Full_Screen_Slider.this.getContentResolver(),
                    b, "1image", "this is downloaded image sample");
                Toast.makeText(mContext, "longpress ", Toast.LENGTH_SHORT).show();

                return true;
            }
        });

        container.addView(itemView);

        return itemView;
    }

it is replacing your view each time it calls instantiateItem so make it final which will help. 它会在每次调用InstantiateItem时替换您的视图,因此将其定为最终将有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM