繁体   English   中英

如何从recylerview中的url下载图像?

[英]How to download a image from url in recylerview?

我制作了一个模因应用程序,它从 API 获取图像 url 并解析它们并使用 Glide 在回收器视图中显示,在我的项目布局中,我有两个按钮,一个用于共享,一个用于下载图像,我希望当用户单击下载时按钮图像下载到用户手机。

帮助我如何实现这一点。

MyAdapter 类

public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {

Context context;
 ArrayList<Model> modelArrayList;

public Adapter(Context context, ArrayList<Model> modelArrayList) {
    this.context = context;
    this.modelArrayList = modelArrayList;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(context).inflate(R.layout.item_layout, parent,false);

    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

    String url = modelArrayList.get(position).getUrl();
      holder.setImage(url);
      holder.button.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              Intent sharing = new Intent (Intent.ACTION_SEND);
              sharing.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
              sharing.setType("text/plain");
              String subject = "Hey Man just look at this coll meme click the link " +url;
              sharing.putExtra(Intent.EXTRA_TEXT,subject);
              context.startActivity(Intent.createChooser(sharing,"Shring using"));
          }
      });

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

          }
      });

}

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

public class  ViewHolder extends RecyclerView.ViewHolder{

    ImageView imageView;
    Button button,buttonDownload;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);

        imageView = itemView.findViewById(R.id.imageView);
        button = itemView.findViewById(R.id.button);
        buttonDownload = itemView.findViewById(R.id.btn_download);
    }

    void setImage(String link){
        Glide.with(context).load(link).into(imageView);
    }
}

}

我的模特班

public class Model {

String url;

public Model(String url) {
    this.url = url;
}

public String getUrl() {
    return url;
}

public void setUrl(String url) {
    this.url = url;
}


}

我的 item_layout

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:id="@+id/imageView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_margin="8dp"
    app:layout_constraintBottom_toTopOf="@+id/button"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/ic_launcher_background"
    tools:ignore="VectorDrawableCompat" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="104dp"
    android:layout_marginLeft="104dp"
    android:layout_marginBottom="28dp"
    android:text="Share"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

<Button
    android:id="@+id/btn_download"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="71dp"
    android:layout_marginRight="71dp"
    android:layout_marginBottom="28dp"
    android:text="Download"
    android:textColor="#0B0B0B"
    app:backgroundTint="#12E71A"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

我的主要活动.java

public class MainActivity extends AppCompatActivity {

 RecyclerView recyclerView;
 Adapter adapter;

 ArrayList<Model> arrayList;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    recyclerView = findViewById(R.id.recylerview_id);

     arrayList = new ArrayList<>();





    String url = "https://meme-api.herokuapp.com/gimme/30";

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
            (Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {


                    try {
                        JSONArray jsonArray = response.getJSONArray("memes");

                        for (int i = 0; i < jsonArray.length(); i++){

                            JSONObject jsonObject = jsonArray.getJSONObject(i);

                            String url = jsonObject.getString("url");

                            Model m = new Model(url);
                            arrayList.add(m);

                        }

                        adapter = new Adapter(MainActivity.this,arrayList);
                        recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
                        recyclerView.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    // TODO: Handle error

                }
            });

// Access the RequestQueue through your singleton class.
    MySingleton.getInstance(this).addToRequestQueue(jsonObjectRequest);

}

}

Glide允许加载一个Bitmap

您可以将Bitmap保存到文件,然后将文件路径传递到另一个屏幕。

在这里你可以找到详细的教程,如何使用 Glide 库将图像存储到文件:

https://medium.com/@akshayranagujjar/how-to-save-image-to-storage-using-glide-in-android-fa26c842f212

并且不要忘记存储权限。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM