是否可以一次将多个文件上传到Firebase存储中

问题描述 投票:0回答:1

在我的图像上传器应用程序中,我目前已设置它,可以使用此代码将单个文件上传到Firebase

 private void chooseImage() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
    }
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK
            && data != null && data.getData() != null )
    {
        filePath = data.getData();
        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
            imageView.setImageBitmap(bitmap);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
 }

private void uploadImage() {

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

    if(filePath != null)
    {
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("Uploading...");
        progressDialog.show();

        StorageReference ref = storageReference.child(myDateString+Gap+ UUID.randomUUID().toString());
        FirebaseDatabase db = FirebaseDatabase.getInstance();
        DatabaseReference rootRef = db.getReference().child("PreviousJobs");
        String d = ref.toString();

        rootRef
                .child(myDateString)
                .setValue(myDateString);

        ref.putFile(filePath)
                .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                        progressDialog.dismiss();
                        Toast.makeText(photoUploader.this, "Uploaded", Toast.LENGTH_SHORT).show();
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        progressDialog.dismiss();
                        Toast.makeText(photoUploader.this, "Failed "+e.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                })
                .addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                        double progress = (100.0*taskSnapshot.getBytesTransferred()/taskSnapshot
                                .getTotalByteCount());
                        progressDialog.setMessage("Uploaded "+(int)progress+"%");
                    }
                });
    }
}

xml看起来像这样

 <Button
    android:id="@+id/btnChoose"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="50dp"
    android:layout_marginTop="40dp"
    android:layout_weight="1"
    android:text="Choose"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/btnUpload"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="40dp"
    android:layout_marginEnd="50dp"
    android:layout_weight="1"
    android:text="Upload"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ImageView
    android:id="@+id/imgView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="171dp"
    android:layout_marginTop="40dp"
    android:layout_marginEnd="168dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/btnChoose" />

什么是将其转换为一次上传多个文件的最佳方法?我是Firebase的存储方面的新手,即时通讯完全失败。香港专业教育学院一直在阅读一些帖子,这是不可能的。确实是这种情况,还是可以解决?

java android firebase firebase-storage
1个回答
1
投票

没有用于发送批次的built into the Firebase Android SDK。但是,您可能会直接utilize the Google Cloud Storage API to send batch requests,尽管有点麻烦。

您需要知道是否要走那条路线的第一件事是Firebase存储“存储桶”的ID。如here所述,您可以不使用gs://即可获取存储区名称,并将其传递给GCS资源。

例如,如果我的存储桶是gs://kato-sandbox.appspot.com,则我使用kato-sandbox.appspot.com

© www.soinside.com 2019 - 2024. All rights reserved.