我想在图库中保存图像并与外部应用程序共享

时间:2018-02-28 09:45:56

标签: java android json android-layout adapter

我设计了一个应用程序,我使用recyclerView显示图像。 我没有点击相机中的图像,它是从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 此布局文件专为组件视图而设计,此文件与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(模态类。)

这是我的getter和setter类文件代码。

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

适配器类文件代码在这里 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();
                }
            });
        }

    }

}

我附上邮件布局截图以便更加了解。

单击下载按钮即可下载图像。 和图像应该可以共享社交应用程序。 我尝试了一些代码,但它没有用。 伙计们,请帮我找到解决方案。 先感谢您。 please find the attachment of this image

1 个答案:

答案 0 :(得分:0)

请参阅以下链接下载或从图像视图保存图像

Save image from ImageView to device gallery

在链接

下方共享图片

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