有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java是否可以一次将多个文件上载到firebase存储

******编辑******

事实证明,你可以在一个实例中上传多个文件。我使用了下面的代码。所有文件都会上传,但由于某些原因,在应用程序中,上传的项目回收视图中只显示一个项目。有人能快速查看一下吗?请解释一下原因

回收物品。xml

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


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

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

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

照片上传器类

     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;
}

最后是适配器类

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);


        }

    }

}

共 (1) 个答案

  1. # 1 楼答案

    没有任何built into the Firebase Android SDK用于发送批处理。不过,您可以直接utilize the Google Cloud Storage API to send batch requests,尽管这有点复杂

    如果你想走这条路,你首先需要知道的是你的Firebase存储“bucket”的id。如here所述,您只需获取不带gs://的bucket名称,并将其传递到GCS资源中

    例如,如果我的bucket是gs://kato-sandbox.appspot.com,那么我使用kato-sandbox.appspot.com

    要在Firebase中查找您的bucket名称,请访问your storage page in console.firebase.google.com并查看列出的URL

    enter image description here