查看来自Firebase存储的照片,在RecyclerView中显示。

huangapple go评论80阅读模式
英文:

View photos from firebase storage in RecyclerView

问题

我已经将您提供的内容翻译如下:

我已经在 Firebase 存储中手动存储了一组图片,我想在 Android Studio 中用 Java 从 Firebase 存储中检索图像,并在 RecyclerView 中的列表中显示它们。我尝试了许多解决方案,但它们都失败了,因为它们没有显示任何内容。

以下是在 fragment 的 onActivityCreated 方法中的代码:

StorageReference mStorageRef = getStorage().getReference();
mStorageRef.listAll().addOnSuccessListener(listResult -> {
    for (StorageReference prefix : listResult.getPrefixes()) {
        if (prefix.getName().equals("1")) {
            prefix.listAll().addOnSuccessListener(listResult1 -> {
                for (StorageReference item : listResult1.getItems()) {
                    item.getBytes(Long.MAX_VALUE).addOnSuccessListener(paths::add).addOnFailureListener(exception -> {
                        // 处理任何错误
                    });
                }
            }).addOnFailureListener(e -> Snackbar.make(recyclerView, getResources().getString(R.string.fail_show) + ": " + e.getMessage(), Snackbar.LENGTH_LONG).show());
        }
    }
}).addOnFailureListener(e -> Snackbar.make(recyclerView, getResources().getString(R.string.fail_ID) + ": " + e.getMessage(), Snackbar.LENGTH_LONG).show());

RecyclerView.LayoutManager layoutManager = new GridLayoutManager(requireContext(), 1);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
RecyclerView.Adapter mAdapter = new AdapterImageUploaded(requireContext(), paths);
recyclerView.setAdapter(mAdapter);
recyclerView.setItemViewCacheSize(mAdapter.getItemCount());

这是适配器的代码:

public class AdapterImageUploaded extends RecyclerView.Adapter<AdapterImageUploaded.MyViewHolder> {
    private ArrayList<byte[]> path_image;
    public Context context;

    public AdapterImageUploaded(Context context, ArrayList<byte[]> path_image) {
        this.path_image = path_image;
        this.context = context;
    }

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

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        Glide.with(holder.item_upload).load(path_image.get(position)).placeholder(R.drawable.fish_2).into(holder.img_fish_uploaded);
    }

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

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        public ImageView img_fish_uploaded;
        public ConstraintLayout item_upload;

        public MyViewHolder(ConstraintLayout linearLayout) {
            super(linearLayout);
            img_fish_uploaded = linearLayout.findViewById(R.id.img_fish_uploaded);
            item_upload = linearLayout.findViewById(R.id.item_upload);
        }
    }
}

问题是代码没有在 RecyclerView 中显示图像。

英文:

I've stored a set of pictures in firebase storage Manually and I want Retrieving image and show them in list in Recycler View in android studio in java, I try many solution in this field but he is fail because He did not display anything
and this code in onActivityCreated - fragment

StorageReference mStorageRef = getStorage().getReference();
mStorageRef.listAll().addOnSuccessListener(listResult -&gt; {
for (StorageReference prefix : listResult.getPrefixes()) {
if (prefix.getName().equals(&quot;1&quot;)) {
prefix.listAll().addOnSuccessListener(listResult1 -&gt; {
for (StorageReference item : listResult1.getItems()) {
item.getBytes(Long.MAX_VALUE).addOnSuccessListener(paths::add).addOnFailureListener(exception -&gt; {
// Handle any errors
});
}
}).addOnFailureListener(e -&gt; Snackbar.make(recyclerView, getResources().getString(R.string.fail_show) + &quot;: &quot; + e.getMessage(), Snackbar.LENGTH_LONG).show());
}
}
}).addOnFailureListener(e -&gt; Snackbar.make(recyclerView, getResources().getString(R.string.fail_ID) + &quot;: &quot; + e.getMessage(), Snackbar.LENGTH_LONG).show());
/*mStorageRef.listAll().addOnSuccessListener(listResult -&gt; paths.addAll(listResult.getItems()))
.addOnFailureListener(e -&gt; Snackbar.make(recyclerView, getResources().getString(R.string.fail_show) + &quot;: &quot; + e.getMessage(), Snackbar.LENGTH_LONG).show());*/
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(requireContext(), 1);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
RecyclerView.Adapter mAdapter = new AdapterImageUploaded(requireContext(), paths);
recyclerView.setAdapter(mAdapter);
recyclerView.setItemViewCacheSize(mAdapter.getItemCount());

this is adapter

public class AdapterImageUploaded extends RecyclerView.Adapter&lt;AdapterImageUploaded.MyViewHolder&gt; {
private ArrayList&lt;byte[]&gt; path_image;
public Context context;
public AdapterImageUploaded(Context context,ArrayList&lt;byte[]&gt; path_image) {
this.path_image = path_image;
this.context=context;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
ConstraintLayout layout = (ConstraintLayout) LayoutInflater.from(parent.getContext()).inflate(R.layout.item_upload_image, parent, false);
return new MyViewHolder(layout);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
Glide.with(holder.item_upload).load(path_image.get(position)).placeholder(R.drawable.fish_2).into(holder.img_fish_uploaded);
}
@Override
public int getItemCount() {
return path_image.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
public ImageView img_fish_uploaded;
public ConstraintLayout item_upload;
public MyViewHolder(ConstraintLayout linearLayout) {
super(linearLayout);
img_fish_uploaded = linearLayout.findViewById(R.id.img_fish_uploaded);
item_upload = linearLayout.findViewById(R.id.item_upload);
}
}
}

**the problem is the code not show image in recyclerview **

答案1

得分: 0

StorageReference mStorageRef = getStorage().getReference();
mStorageRef.listAll().addOnSuccessListener(listResult -> {
    for (StorageReference prefix : listResult.getPrefixes()) {
        if (prefix.getName().equals("1")) {
            prefix.listAll().addOnSuccessListener(listResult1 -> {
                for (StorageReference item : listResult1.getItems()) {
                    paths.add(item);
                }
                RecyclerView.LayoutManager layoutManager = new GridLayoutManager(requireContext(), 1);
                recyclerView.setLayoutManager(layoutManager);
                recyclerView.setHasFixedSize(true);
                RecyclerView.Adapter mAdapter = new AdapterImageUploaded(requireContext(), paths);
                recyclerView.setAdapter(mAdapter);
                recyclerView.setItemViewCacheSize(mAdapter.getItemCount());
            }).addOnFailureListener(e -> Snackbar.make(recyclerView, getResources().getString(R.string.fail_show) + ": " + e.getMessage(), Snackbar.LENGTH_LONG).show());
        }
    }
}).addOnFailureListener(e -> Snackbar.make(recyclerView, getResources().getString(R.string.fail_ID) + ": " + e.getMessage(), Snackbar.LENGTH_LONG).show());
英文:
StorageReference mStorageRef = getStorage().getReference();
mStorageRef.listAll().addOnSuccessListener(listResult -&gt; {
for (StorageReference prefix : listResult.getPrefixes()) {
if (prefix.getName().equals(&quot;1&quot;)) {
prefix.listAll().addOnSuccessListener(listResult1 -&gt; {
for (StorageReference item : listResult1.getItems()) {
paths.add(item)
}
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(requireContext(), 1);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
RecyclerView.Adapter mAdapter = new AdapterImageUploaded(requireContext(), paths);
recyclerView.setAdapter(mAdapter);
recyclerView.setItemViewCacheSize(mAdapter.getItemCount());
}).addOnFailureListener(e -&gt; Snackbar.make(recyclerView, getResources().getString(R.string.fail_show) + &quot;: &quot; + e.getMessage(), Snackbar.LENGTH_LONG).show());
}
}
}).addOnFailureListener(e -&gt; Snackbar.make(recyclerView, getResources().getString(R.string.fail_ID) + &quot;: &quot; + e.getMessage(), Snackbar.LENGTH_LONG).show());

huangapple
  • 本文由 发表于 2020年10月3日 18:43:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/64183334.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定