简体   繁体   中英

is it possible to upload multiple files to firebase storage at once

******EDIT******

Its turns out you can upload multiple files in one instance.Ive used the code below.All the files upload but for some reason in-app it only shows one item in the uploaded items recycleview. Can somebody just have a quick check through n pehaps explain why please

recycle_item.xml

 android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:orientation="horizontal"
    android:padding="10dp"
    android:weightSum="10">


    <ImageView
        android:id="@+id/upload_icon"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_weight="2"
        android:src="@drawable/ic_attach_file_black_24dp"/>

    <TextView
        android:id="@+id/upload_filename"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="6"

        android:gravity="center_vertical"
        android:text="Filename.type"
        android:textSize="16sp" />

photoUploader class

     initVariables();

}
private void initVariables() {
    mStorage = FirebaseStorage.getInstance().getReference();

    mSelectBtn = findViewById(R.id.select_btn);
    mUploadList = findViewById(R.id.upload_list);

    mUploadList = (RecyclerView) findViewById(R.id.upload_list);

    fileNameList = new ArrayList<>();
    fileDoneList = new ArrayList<>();

    uploadListAdapter = new UploadListAdapter(fileNameList, fileDoneList);

    //RecyclerView

    mUploadList.setLayoutManager(new LinearLayoutManager(this));
    mUploadList.setHasFixedSize(true);
    mUploadList.setAdapter(uploadListAdapter);

    mSelectBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
    chooseImage();
        }
    });
}

  private void chooseImage() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent,"Select Picture"), 
RESULT_LOAD_IMAGE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent 
    data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK){

        if(data.getClipData() != null){
            int totalItemsSelected = data.getClipData().getItemCount();

            for(int i = 0; i < totalItemsSelected; i++){

                Uri fileUri = data.getClipData().getItemAt(i).getUri();

                String fileName = getFileName(fileUri);

                fileNameList.add(fileName);
                fileDoneList.add("uploading");
                uploadListAdapter.notifyDataSetChanged();

                Date myDate = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                final String myDateString = sdf.format(myDate);
                String Gap ="/";


                StorageReference fileToUpload = mStorage.child(myDateString+Gap+ UUID.randomUUID().toString());

                final int finalI = i;
                fileToUpload.putFile(fileUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                        FirebaseDatabase sb = FirebaseDatabase.getInstance();
                        DatabaseReference rootRef = sb.getReference().child("PreviousJobs");


                        rootRef
                                .child(myDateString)
                                .setValue(myDateString);
                        fileDoneList.remove(finalI);
                        fileDoneList.add(finalI, "done");

                        uploadListAdapter.notifyDataSetChanged();

                    }
                });

            }

            //Toast.makeText(MainActivity.this, "Selected Multiple Files", Toast.LENGTH_SHORT).show();

        } else if (data.getData() != null){

            Toast.makeText(photoUploader.this, "Selected Single File", Toast.LENGTH_SHORT).show();

        }

    }

}

    public String getFileName(Uri uri) {
    String result = null;
    if (uri.getScheme().equals("content")) {
        Cursor cursor = getContentResolver().query(uri, null, null, null, 
    null);
        try {
            if (cursor != null && cursor.moveToFirst()) {
                result = 
     cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
             }
        } finally {
            cursor.close();
        }
    }
    if (result == null) {
        result = uri.getPath();
        int cut = result.lastIndexOf('/');
        if (cut != -1) {
            result = result.substring(cut + 1);
        }
    }
    return result;
}

finally the adapter class

public List<String> fileNameList;
    public List<String> fileDoneList;

public UploadListAdapter(List<String> fileNameList, List<String>fileDoneList){

        this.fileDoneList = fileDoneList;
        this.fileNameList = fileNameList;

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycle_item, parent, false);
        return new ViewHolder(v);

    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        String fileName = fileNameList.get(position);
        holder.fileNameView.setText(fileName);

        String fileDone = fileDoneList.get(position);

        if(fileDone.equals("uploading")){

            holder.fileDoneView.setImageResource(R.drawable.progress);

        } else {

            holder.fileDoneView.setImageResource(R.drawable.checked);

        }

    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {

        View mView;

        public TextView fileNameView;
        public ImageView fileDoneView;

        public ViewHolder(View itemView) {
            super(itemView);

            mView = itemView;

            fileNameView = (TextView) mView.findViewById(R.id.upload_filename);
            fileDoneView = (ImageView) mView.findViewById(R.id.upload_loading);


        }

    }

}

There is nothing built into the Firebase Android SDK for sending batches. However, you could directlyutilize the Google Cloud Storage API to send batch requests , although it's a bit involved.

The first thing you need to know if you want to go that route is id for your Firebase storage "bucket". As described here , you can just take the bucket name without gs:// and pass that into the GCS resources.

For example, if my bucket is gs://kato-sandbox.appspot.com then I use kato-sandbox.appspot.com .

To look up your bucket name in Firebase, visit your storage page in console.firebase.google.com and look at the URL listed.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM