英文:
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 -> {
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 -> {
// Handle any errors
});
}
}).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());
/*mStorageRef.listAll().addOnSuccessListener(listResult -> paths.addAll(listResult.getItems()))
.addOnFailureListener(e -> Snackbar.make(recyclerView, getResources().getString(R.string.fail_show) + ": " + 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<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);
}
}
}
**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 -> {
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());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论