📌  相关文章
📜  如何查看Firebase存储中的所有已上传图像?

📅  最后修改于: 2021-05-09 16:56:17             🧑  作者: Mango

在本文中,我们将从Firebase存储中加载所有图像,并在RecyclerView中显示它们。通常,我们在向实时数据库添加链接后显示图像。假设我们要显示所有上传的图像,就像我们在图库中看到的那样。我们将只查看所有图像。因此,在这里我们将显示在Firebase存储中上载的特定文件夹中的图像。但是在这里,我们将在Arraylist中添加所有图像URL之后显示图像。

分步实施

步骤1:创建一个新项目

要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Java作为编程语言。

步骤2:将其添加到AndroidManifest.xml文件中

将此添加到build.gradle文件中

implementation 'com.google.firebase:firebase-storage:19.1.1'
implementation 'com.github.bumptech.glide:glide:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'

步骤3:使用item.xml文件

转到应用程序> res>布局>新建>布局资源文件,并将文件命名为item 。转到item.xml文件,并参考以下代码。以下是item.xml文件的代码。

XML


   

   


Java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
 
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
 
import com.anni.uploaddataexcelsheet.R;
import com.bumptech.glide.Glide;
 
import java.util.ArrayList;
 
public class ImageAdapter extends RecyclerView.Adapter {
 
    private ArrayList imageList;
 
    public ImageAdapter(ArrayList imageList, Context context) {
        this.imageList = imageList;
        this.context = context;
    }
 
    private Context context;
    @NonNull
    @Override
    public ImageAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item,parent,false);
        return new ViewHolder(view);
    }
 
    @Override
    public void onBindViewHolder(@NonNull ImageAdapter.ViewHolder holder, int position) {
      // laoding the images from the position 
      Glide.with(holder.itemView.getContext()).load(imageList.get(position)).into(holder.imageView);
    }
 
    @Override
    public int getItemCount() {
        return imageList.size();
    }
 
    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView=itemView.findViewById(R.id.item);
        }
    }
}


XML


   

   
    
   


Java
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
 
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
 
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.storage.FileDownloadTask;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.ListResult;
import com.google.firebase.storage.StorageReference;
 
import java.util.ArrayList;
 
public class ShowAllImagesFromStorage extends AppCompatActivity {
    ArrayList imagelist;
    RecyclerView recyclerView;
    StorageReference root;
    ProgressBar progressBar;
    ImageAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_all_images_from_storage);
        imagelist=new ArrayList<>();
        recyclerView=findViewById(R.id.recyclerview);
        adapter=new ImageAdapter(imagelist,this);
        recyclerView.setLayoutManager(new LinearLayoutManager(null));
        progressBar=findViewById(R.id.progress);
        progressBar.setVisibility(View.VISIBLE);
        StorageReference listRef = FirebaseStorage.getInstance().getReference().child("images");
        listRef.listAll().addOnSuccessListener(new OnSuccessListener() {
            @Override
            public void onSuccess(ListResult listResult) {
                for(StorageReference file:listResult.getItems()){
                    file.getDownloadUrl().addOnSuccessListener(new OnSuccessListener() {
                        @Override
                        public void onSuccess(Uri uri) {
                            imagelist.add(uri.toString());
                            Log.e("Itemvalue",uri.toString());
                        }
                    }).addOnSuccessListener(new OnSuccessListener() {
                        @Override
                        public void onSuccess(Uri uri) {
                            recyclerView.setAdapter(adapter);
                            progressBar.setVisibility(View.GONE);
                        }
                    });
                }
            }
        });
    }
}


步骤4:使用ImageAdapter。 Java文件

在android studio中创建一个新的Java类,并将该类命名为ImageAdapter。转到ImageAdapter。 Java文件并参考以下代码。下面是ImageAdapter的代码。 Java文件。

Java

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
 
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
 
import com.anni.uploaddataexcelsheet.R;
import com.bumptech.glide.Glide;
 
import java.util.ArrayList;
 
public class ImageAdapter extends RecyclerView.Adapter {
 
    private ArrayList imageList;
 
    public ImageAdapter(ArrayList imageList, Context context) {
        this.imageList = imageList;
        this.context = context;
    }
 
    private Context context;
    @NonNull
    @Override
    public ImageAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item,parent,false);
        return new ViewHolder(view);
    }
 
    @Override
    public void onBindViewHolder(@NonNull ImageAdapter.ViewHolder holder, int position) {
      // laoding the images from the position 
      Glide.with(holder.itemView.getContext()).load(imageList.get(position)).into(holder.imageView);
    }
 
    @Override
    public int getItemCount() {
        return imageList.size();
    }
 
    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView=itemView.findViewById(R.id.item);
        }
    }
}

步骤5:使用activity_showallimages.xml文件

您可以使用MainActivity ,也可以在android studio中创建另一个新的空方法。转到activity_showallimages.xml文件,并参考以下代码。以下是activity_showallimages.xml文件的代码

XML格式



   

   
    
   

步骤6:使用ShowAllImagesFromStorage。 Java文件

转到ShowAllImagesFromStorage。 Java文件并参考以下代码。下面是ShowAllImagesFromStorage的代码。 Java文件。将项目加载到Firebase存储中。

listRef.listAll().addOnSuccessListener(new OnSuccessListener() {
            @Override
            public void onSuccess(ListResult listResult) {
                for(StorageReference file:listResult.getItems()){
                    file.getDownloadUrl().addOnSuccessListener(new OnSuccessListener() {
                        @Override
                        public void onSuccess(Uri uri) {
                            // adding the url in the arraylist
                            imagelist.add(uri.toString());
                            Log.e("Itemvalue",uri.toString());
                        }
                    }).addOnSuccessListener(new OnSuccessListener() {
                        @Override
                        public void onSuccess(Uri uri) {
                            recyclerView.setAdapter(adapter);
                            progressBar.setVisibility(View.GONE);
                        }
                    });
                }
            }
        });

以下是ShowAllImagesFromStorage完整代码。 Java文件。

Java

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
 
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
 
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.storage.FileDownloadTask;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.ListResult;
import com.google.firebase.storage.StorageReference;
 
import java.util.ArrayList;
 
public class ShowAllImagesFromStorage extends AppCompatActivity {
    ArrayList imagelist;
    RecyclerView recyclerView;
    StorageReference root;
    ProgressBar progressBar;
    ImageAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_all_images_from_storage);
        imagelist=new ArrayList<>();
        recyclerView=findViewById(R.id.recyclerview);
        adapter=new ImageAdapter(imagelist,this);
        recyclerView.setLayoutManager(new LinearLayoutManager(null));
        progressBar=findViewById(R.id.progress);
        progressBar.setVisibility(View.VISIBLE);
        StorageReference listRef = FirebaseStorage.getInstance().getReference().child("images");
        listRef.listAll().addOnSuccessListener(new OnSuccessListener() {
            @Override
            public void onSuccess(ListResult listResult) {
                for(StorageReference file:listResult.getItems()){
                    file.getDownloadUrl().addOnSuccessListener(new OnSuccessListener() {
                        @Override
                        public void onSuccess(Uri uri) {
                            imagelist.add(uri.toString());
                            Log.e("Itemvalue",uri.toString());
                        }
                    }).addOnSuccessListener(new OnSuccessListener() {
                        @Override
                        public void onSuccess(Uri uri) {
                            recyclerView.setAdapter(adapter);
                            progressBar.setVisibility(View.GONE);
                        }
                    });
                }
            }
        });
    }
}

输出:

Firebase存储中的已上传文件: