图像未从 firebase 获取

问题描述

我尝试了创建自定义适配器类和认 FirebaseRecyclerAdapter 的常规方法,但图像加载效率不高。

目的是从firebase加载图片但是消息队列很长,请提出一些解决方案。

在这个 MainActivity.java 文件中,我使用了 FirebaseRecyclerAdapter。

public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private GridLayoutManager gridLayoutManager;
private RecyclerView.LayoutManager layoutManager;
private FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
private DatabaseReference databaseReference = firebaseDatabase.getReference();
private DatabaseReference imagesReference = databaseReference.child("Images");




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

        gridLayoutManager = new GridLayoutManager(this,2,GridLayoutManager.VERTICAL,false);



        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setHasFixedSize(true);
    }

    @Override
    protected void onStart() {
        super.onStart();
        FirebaseRecyclerAdapter<ImageUrl,MyViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<ImageUrl,MyViewHolder>(
         new FirebaseRecyclerOptions.Builder<ImageUrl>().setQuery(imagesReference.limitToLast(50),ImageUrl.class).build()
        ) {
            @Override
            protected void onBindViewHolder(@NonNull MyViewHolder holder,int position,@NonNull ImageUrl model) {
                holder.setDetails(getApplicationContext(),model.getUrl());
            }

            @NonNull
            @Override
            public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent,int viewType) {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_grid_layout,parent,false);
                MyViewHolder myViewHolder = new MyViewHolder(view);
                return myViewHolder;
            }
        };

        recyclerView.setAdapter(firebaseRecyclerAdapter);
    }
}

这里我使用了毕加索来加载图像,

MyViewHolder.java 文件

import android.content.Context;
import android.media.Image;
import android.net.Uri;
import android.view.View;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.squareup.picasso.Picasso;

public class MyViewHolder extends RecyclerView.ViewHolder {

    View mView;
    ImageView imageView;
    public MyViewHolder(@NonNull View itemView) {
        super(itemView);
        imageView = itemView.findViewById(R.id.imageView);
    }

    public void setDetails(Context mContext,String url){
        Picasso.get().load(Uri.parse(url)).into(imageView);
    }
}

我是这样进入 logcat 的:

2021-04-14 00:47:19.148 4019-4048/com.example.thoughtsdemo E/ion: ioctl c0044901 Failed with code -1: Invalid argument
2021-04-14 00:47:19.475 4019-4045/com.example.thoughtsdemo W/System: ClassLoader referenced unkNown path: system/framework/mediatek-cta.jar
2021-04-14 00:47:19.477 4019-4045/com.example.thoughtsdemo I/System.out: e:java.lang.classNotFoundException: com.mediatek.cta.CtaHttp
2021-04-14 00:47:19.950 4019-4045/com.example.thoughtsdemo D/OpenSsllib: OpensslErr:Module:12(177:); file:external/boringssl/src/crypto/asn1/asn1_lib.c ;Line:168;Function:ASN1_get_object
2021-04-14 00:47:19.951 4019-4045/com.example.thoughtsdemo D/OpenSsllib: OpensslErr:Module:12(177:); file:external/boringssl/src/crypto/asn1/asn1_lib.c ;Line:168;Function:ASN1_get_object

在红色的 logcat 中,它显示如下:

2021-04-14 13:31:51.560 30382-30382/com.example.thoughtsdemo E/ANR_LOG: >>> msg's executing time is too long
2021-04-14 13:31:51.560 30382-30382/com.example.thoughtsdemo E/ANR_LOG: Blocked msg = { when=-1m33s688ms what=159 target=android.app.ActivityThread$H obj=android.app.servertransaction.ClientTransaction@fb63a696 },cost  = 90743 ms
2021-04-14 13:31:51.560 30382-30382/com.example.thoughtsdemo E/ANR_LOG: >>>Current msg List is:
2021-04-14 13:31:51.561 30382-30382/com.example.thoughtsdemo E/ANR_LOG: Current msg <1>  = { when=-1m33s432ms what=149 target=android.app.ActivityThread$H obj=android.os.BinderProxy@622d88c }
2021-04-14 13:31:51.562 30382-30382/com.example.thoughtsdemo E/ANR_LOG: Current msg <2>  = { when=-1m30s168ms what=0 target=android.os.Handler callback=androidx.core.content.res.ResourcesCompat$FontCallback$2 }
2021-04-14 13:31:51.562 30382-30382/com.example.thoughtsdemo E/ANR_LOG: Current msg <3>  = { when=-1m30s162ms what=0 target=android.os.Handler callback=androidx.core.content.res.ResourcesCompat$FontCallback$2 }
2021-04-14 13:31:51.563 30382-30382/com.example.thoughtsdemo E/ANR_LOG: Current msg <4>  = { when=-1m20s754ms what=132 target=android.app.ActivityThread$H }
2021-04-14 13:31:51.563 30382-30382/com.example.thoughtsdemo E/ANR_LOG: Current msg <5>  = { when=-26ms what=0 target=android.view.ViewRootImpl$ViewRootHandler callback=android.view.ViewRootImpl$4 }
2021-04-14 13:31:51.563 30382-30382/com.example.thoughtsdemo E/ANR_LOG: Current msg <6>  = { when=-9ms barrier=0 }
2021-04-14 13:31:51.563 30382-30382/com.example.thoughtsdemo E/ANR_LOG: >>>CURRENT MSG DUMP OVER<<<

ImageUrl.java :

public class ImageUrl {

String url;

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

public ImageUrl(){

}

public String getUrl() {
    return url;
}

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

}

我使用 Log.d() 在 onBindViewHolder 中显示获取的 url,我没有打印 URL,所以问题在这里

 protected void onBindViewHolder(@NonNull MyViewHolder holder,@NonNull ImageUrl model) {
               Log.d("url",model.getUrl());
                holder.setDetails(getApplicationContext(),model.getUrl());

            }

当我使用 Glide 时,在 logcat 中用 Red 写的东西,

    2021-04-14 17:28:03.148 16789-16789/com.example.thoughtsdemo E/ANR_LOG: >>> msg's executing time is too long
2021-04-14 17:28:03.148 16789-16789/com.example.thoughtsdemo E/ANR_LOG: Blocked msg = { when=-34s879ms what=159 target=android.app.ActivityThread$H obj=android.app.servertransaction.ClientTransaction@7f3731c5 },cost  = 31829 ms
2021-04-14 17:28:03.148 16789-16789/com.example.thoughtsdemo E/ANR_LOG: >>>Current msg List is:
2021-04-14 17:28:03.149 16789-16789/com.example.thoughtsdemo E/ANR_LOG: Current msg <1>  = { when=-34s610ms what=149 target=android.app.ActivityThread$H obj=android.os.BinderProxy@622d88c }
2021-04-14 17:28:03.149 16789-16789/com.example.thoughtsdemo E/ANR_LOG: Current msg <2>  = { when=-31s140ms what=0 target=android.os.Handler callback=androidx.core.content.res.ResourcesCompat$FontCallback$2 }
2021-04-14 17:28:03.150 16789-16789/com.example.thoughtsdemo E/ANR_LOG: Current msg <3>  = { when=-31s134ms what=0 target=android.os.Handler callback=androidx.core.content.res.ResourcesCompat$FontCallback$2 }
2021-04-14 17:28:03.150 16789-16789/com.example.thoughtsdemo E/ANR_LOG: Current msg <4>  = { when=-21s840ms what=132 target=android.app.ActivityThread$H }
2021-04-14 17:28:03.151 16789-16789/com.example.thoughtsdemo E/ANR_LOG: Current msg <5>  = { when=-37ms what=0 target=android.view.ViewRootImpl$ViewRootHandler callback=android.view.ViewRootImpl$4 }
2021-04-14 17:28:03.151 16789-16789/com.example.thoughtsdemo E/ANR_LOG: Current msg <6>  = { when=-10ms barrier=0 }
2021-04-14 17:28:03.151 16789-16789/com.example.thoughtsdemo E/ANR_LOG: >>>CURRENT MSG DUMP OVER<<<

请帮忙。谢谢。

解决方法

使用滑行

添加依赖:

  implementation 'com.github.bumptech.glide:glide:4.12.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'

如何加载图片:

Glide.with(CONTEXT).load(URL).into(IMAGE_VIEW);

注意:替换为您自己的值。

如有不清楚的地方,请随时提问。