如何从recylerview中的url下载图像? [英] How to download a image from url in recylerview?

查看:44
本文介绍了如何从recylerview中的url下载图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个 meme 应用程序,它从 API 获取图像 url 并对其进行解析并使用 Glide 在回收器视图中显示,在我的项目布局中,我有两个按钮,一个用于共享,一个用于下载图像,我想要用户单击下载按钮,图像将下载到用户手机.

I have made a meme app, which fetches image url from an API and i m parsing them and showing in recyler view using Glide , in my item layout i have two buttons one for sharing and one for download Image , i want when user clicks the download button the images get download to the user phone.

帮助我如何实现这一点.

Help me how i can implement this.

MyAdapter 类

MyAdapter class

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

My 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>

</androidx.constraintlayout.widget.ConstraintLayout>

我的 mainactivity.java

My mainactivity.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保存到文件中,然后将文件路径传递到另一个屏幕.

you can save Bitmap to file, and then just pass file path to another screen.

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

Here you can find detailed tutorial, how to store image to file with Glide library:

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

不要忘记存储权限.

这篇关于如何从recylerview中的url下载图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆