2016-08-23 13 views
1

私はrecycleview androidのcardviewから画像ビューを共有して保存しようとしています。 私のimagviewは静的なファイルで、アンドロイドギャラリーにこのファイルを保存し、画像共有ボタンをクリックしたときにこの画像を共有します。 下記のスナップをご覧ください! enter image description hereImageViewをRecycleview AndroidのCardviewから保存し、共有する

マイコードは以下のとおりです。

public class PicCardViewHolder extends RecyclerView.ViewHolder { 

public TextView pic_name; 
public ImageView st_image; 
public ImageView save_img; 
public ImageView share_st_img; 
public Drawable drawable; 
public Bitmap bitmap; 
public String ImagePath; 
public Uri URI; 

public Context context; 


public PicCardViewHolder(View v) { 
    super(v); 
    context = itemView.getContext(); 
    pic_name = (TextView) itemView.findViewById(R.id.pic_name); 
    st_image = (ImageView) itemView.findViewById(R.id.st_pic); 
    save_img = (ImageView) itemView.findViewById(R.id.down_img); 
    share_st_img = (ImageView) itemView.findViewById(R.id.share_pic_st); 

    v.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      int pos = getAdapterPosition(); 
      if (pos == 0) { 

       //code for save image into gallery android 
       st_image.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View view) { 

         drawable = ContextCompat.getDrawable(context, R.drawable.test_image); 
         bitmap = ((BitmapDrawable) drawable).getBitmap(); 
         ImagePath = MediaStore.Images.Media.insertImage(
           context.getContentResolver(), 
           bitmap, 
           "demo_image", 
           "demo_image" 
         ); 

         URI = Uri.parse(ImagePath); 
         Toast.makeText(context, "Image Saved Successfully", Toast.LENGTH_LONG).show(); 


        } 
       }); 

      } 
     } 
    }); 
} 
} 

答えて

0

私は私の問題を解決:)

v.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      int pos = getAdapterPosition(); 
      if (pos == 0) { 

       share_st_img.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View view) { 
// 
         drawable = ContextCompat.getDrawable(context, R.drawable.test_image); 
         bitmap = ((BitmapDrawable) drawable).getBitmap(); 
         ImagePath = MediaStore.Images.Media.insertImage(
           context.getContentResolver(), 
           bitmap, 
           "demo_image", 
           "demo_image" 
         ); 

         URI = Uri.parse(ImagePath); 
         Toast.makeText(context, "Image GET Successfully", Toast.LENGTH_LONG).show(); 
         //share image code 
         Intent shareIntent = new Intent(Intent.ACTION_SEND); 
         Uri photoUri = Uri.parse(ImagePath); 
         shareIntent.setData(photoUri); 
         shareIntent.setType("image/png"); 
         shareIntent.putExtra(Intent.EXTRA_STREAM, photoUri); 
         context.startActivity(Intent.createChooser(shareIntent, "Use this for sharing")); 
        } 
       }); 

       save_img.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View view) { 

         drawable = ContextCompat.getDrawable(context, R.drawable.test_image); 
         bitmap = ((BitmapDrawable) drawable).getBitmap(); 
         ImagePath = MediaStore.Images.Media.insertImage(
           context.getContentResolver(), 
           bitmap, 
           "demo_image", 
           "demo_image" 
         ); 

          URI = Uri.parse(ImagePath); 
          Toast.makeText(context, "Saved Successfully to Gallery", Toast.LENGTH_LONG).show(); 
//        addImageToGallery(context, ImagePath, "he", "hi"); 
         String root =  Environment.getExternalStorageDirectory().toString(); 
         File myDir = new File(root + "/StatusQuotes Images"); 
         myDir.mkdirs(); 
         Random generator = new Random(); 
         int n = 10000; 
         n = generator.nextInt(n); 
         String fname = "Image-" + n + ".jpg"; 
         File file = new File(myDir, fname); 
         Log.i(fname, "" + file); 
         if (file.exists()) 
          file.delete(); 
         try { 
          FileOutputStream out = new FileOutputStream(file); 
          bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out); 
          out.flush(); 
          out.close(); 
         } catch (Exception e) { 
          e.printStackTrace(); 
         } 
         MediaScannerConnection.scanFile(context, new String[]{file.toString()}, null, 
           new MediaScannerConnection.OnScanCompletedListener() { 
            public void onScanCompleted(String path, Uri uri) { 
             Log.i("ExternalStorage", "Scanned " + path + ":"); 
             Log.i("ExternalStorage", "-> uri=" + uri); 
            } 
           }); 
        } 
       }); 


      } 
関連する問題