在 Recycleview 中保存和共享来自 Cardview 的 ImageView Android

Save and Share ImageView from Cardview in Recycleview Android

我正在尝试在 recycleview android 中共享和保存来自 cardview 的图像视图。 我的 imagview 是一个静态文件,我想将此文件保存在 android 图库中,并在我单击共享图像按钮时共享此图像。 请看下面的快照!

我的代码在下面!

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


                    }
                });

            }
        }
    });
}
}

我的问题解决了:)

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


            }