2017-04-06 84 views
0

我設法使用onlongclicklistner()在下面給出的代碼的幫助下將圖像從imageview保存到圖庫。但問題是,它總是保存最後的圖像劑量問題,我試圖保存圖像。在viewpager中將圖像從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從可繪製的陣列獲取圖像將圖像

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

} 
+0

檢查** onLongClickListener()**總是返回相同的數據還是不同的?代碼看起來很好。 –

+0

發表你的onLongClickListener()代碼 –

+0

你如何在imgeView上放置多個圖像你可以顯示嗎?可能會使imageView final可以解決這個問題。它會爲每個imageView製作單獨的對象。 – 9spl

回答

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,所以最終它會有所幫助。

+0

謝謝老兄只是一個快速的問題,你能告訴我在哪裏把進度對話框在此代碼顯示圖像下載和隱藏在圖像存儲 – bilal

+0

您可以查看資源就緒,您可以在其中隱藏進度條https://github.com/bumptech/glide/wiki/Custom-targets – bilal

+0

嘗試用戶滑翔加載圖像inimageview這將給它的工作:-) – 9spl

相關問題