将图像从viewview中的imageview保存到图库

时间:2017-04-06 05:05:30

标签: java android imageview

我设法使用onlongclicklistner()在下面给出的代码的帮助下将图像从imageview保存到图库。但问题是它总是保存最后的图像剂量,这是我试图保存的图像。

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;
            }
        }
    }

我将来自viewpager的图片从drawables数组中获取图像

class 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);
    }

}

1 个答案:

答案 0 :(得分:0)

替换此

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

它每次调用instantiateItem时都会替换你的视图,所以将它设置为final将有所帮助。