-4

I design one app where I am displaying image using recyclerView. I am not clicking image from camera, its downloded from json api. activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginRight="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="16dp"
    tools:context="com.bjp.app.bjpapp.MainActivity">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/scrollView">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    </ScrollView>
</RelativeLayout>

layout_design.xml this layout file designed for the component views and this file attaching with recyclerView.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:layout_marginBottom="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginLeft="16dp">
<ImageView
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:id="@+id/image"
        android:src="@drawable/logo"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@+id/image"
        android:layout_weight=".2"
        android:layout_marginTop="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:orientation="horizontal"
        android:weightSum="2">
        <ImageButton
            android:id="@+id/share"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:src="@drawable/share"
            android:layout_marginRight="5dp"
            android:theme="@style/MyButton1"/>
        <ImageButton
            android:id="@+id/download"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:src="@drawable/downward"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:theme="@style/MyButton1"/>
    </LinearLayout>
</RelativeLayout>

Data.java (Modal class.)

this is my getter and setter class file code.

public class Data implements Serializable {
    private int id;
    private String imgUrl;

    public Data(int id, String imgUrl) {
        this.id = id;
        this.imgUrl = imgUrl;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getImgUrl() {
        return imgUrl;
    }

    public void setImgUrl(String imgUrl) {
        this.imgUrl = imgUrl;
    }
}

Adapter class file code is here Image_Adaper.java

public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ViewHolder> {
    private List<Data> data;
    private Context context;
    private Bitmap bitmap;
    String url1;
    String filepath;
    public ImageAdapter(List<Data> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_design,parent,false);
        return new ImageAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final Data data1=data.get(position);
         url1=data1.getImgUrl();
        ImageView imageView=holder.imageView;
        Glide.with(context)
                .load(data1.getImgUrl())
                .placeholder(R.drawable.logo)
                .into(imageView);
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
    public ImageView imageView;
    public ImageButton share,download;
        public ViewHolder(final View itemView) {
            super(itemView);
            imageView=(ImageView)itemView.findViewById(R.id.image);
            share=(ImageButton)itemView.findViewById(R.id.share);
            download=(ImageButton)itemView.findViewById(R.id.download);

            share.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position=getAdapterPosition();
                    Data data2=data.get(position);
                    // i want to share a image with social sites
                    Toast.makeText(context,"Product id "+data2+"  btn working",Toast.LENGTH_LONG).show();
                }
            });

            download.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context,"download btn working"+url1,Toast.LENGTH_LONG).show();
                }
            });
        }

    }

}

I am attaching the mail layout screenshot for more understanding.

The image should be downloaded on click of the download button. and Image should be shareable to social apps. I tried some code but it is not working. Please, guys, help me to find solutions. Thank you in advance. please find the attachment of this image

Anuj Mourya
  • 21
  • 1
  • 9
  • if you google it, lots of example available. – Hemant Parmar Feb 28 '18 at 09:47
  • What did you try to save and share the image? I see no code done inside OnClickListener – Rakshit Nawani Feb 28 '18 at 09:48
  • Possible duplicate of [Android: Save image on SD card with its original size](https://stackoverflow.com/questions/12579084/android-save-image-on-sd-card-with-its-original-size) – Hemant Parmar Feb 28 '18 at 09:49
  • String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString(); File myDir = new File(root + "/saved_images_1"); int n = 10000; n = generator.nextInt(n); String fname = "Image-" + n + ".jpg"; File file = new File(myDir, fname); if (file.exists()) file.delete(); try { FileOutputStream out = new FileOutputStream(file); finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out); out.flush(); out.close(); – Anuj Mourya Feb 28 '18 at 09:49
  • @HemantParmar , No I am not capturing image from camera. I am downloading image from api and displaying it on recyclerView . – Anuj Mourya Feb 28 '18 at 09:51

1 Answers1

0

Please refer below link for download or save image from image view

Save image from ImageView to device gallery

For Sharing image foloww below link

https://developer.android.com/training/sharing/send.html