在Firebase Android中上传多个图像URL

时间:2019-12-22 06:48:47

标签: java android firebase firebase-storage

我正在尝试将多个图像上传到Firebase,用户可以在其中选择两种类型的图像选择。 一种是用户可以选择文档首页图像,另一种是图像的背面以及一些用户数据。我只上传了一张图片到Firebase。不知道如何同时上传两个图像。 我的文档上传页面将是这样。我也添加了图像。 Document upload page image my database image它只存储一个图像,我想在firebase中存储多个图像url

这是我的代码

UserDocument.java

按钮onclick列表器

    dl_back.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            isDlFront=false;
            isDlBack=true;

            SelectImage();
        }
    });

      dl_front.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            isDlBack=false;
            isDlFront=true;

            SelectImage();
        }
    });

图像选择

    private void SelectImage(){

    final CharSequence[] items={"Camera","Gallery", "Cancel"};

    AlertDialog.Builder builder = new AlertDialog.Builder(UserDocument.this);
    builder.setTitle("Add Image");

    builder.setItems(items, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            if (items[i].equals("Camera")) {

                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, REQUEST_CAMERA);

            } else if (items[i].equals("Gallery")) {

                Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                intent.setType("image/*");
                //startActivityForResult(intent.createChooser(intent, "Select File"), SELECT_FILE);
                startActivityForResult(intent, SELECT_FILE);

            } else if (items[i].equals("Cancel")) {
                dialogInterface.dismiss();
            }
        }
    });
    builder.show();

}
@Override
public  void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode,data);

    if(resultCode== Activity.RESULT_OK){

        if(requestCode==REQUEST_CAMERA){

            saveUri=data.getData();


            Bundle bundle = data.getExtras();
            final Bitmap bmp = (Bitmap) bundle.get("data");
            if(isDlFront==true)
                dlFront.setImageBitmap(bmp);
            else if(isDlBack==true)
                dlBack.setImageBitmap(bmp);

        }else if(requestCode==SELECT_FILE){




            saveUri = data.getData();

            imageUri.add(saveUri);

            //Toast.makeText(UserDocument.this,""+saveUri,Toast.LENGTH_LONG).show();
            if(isDlFront==true)
                dlFront.setImageURI(saveUri);
            else if(isDlBack==true)
                dlBack.setImageURI(saveUri);
        }

    }
}

上传图片

     private void uploadImage() {

    if(saveUri!=null){
        final ProgressDialog mDialog=new ProgressDialog(this);
        mDialog.setMessage("Uploading...");
        mDialog.show();




        String imageName= UUID.randomUUID().toString();
        final StorageReference imageFolder=storageReference.child("bike/"+imageName);




        //           Toast.makeText(UserDocument.this,""+saveUri,Toast.LENGTH_LONG).show();
        imageFolder.putFile(saveUri)
                .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                        mDialog.dismiss();
                        Toast.makeText(UserDocument.this,"Uploaded !!!",Toast.LENGTH_SHORT).show();
                        imageFolder.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                            @Override
                            public void onSuccess(Uri uri) {

                                currDocument = new CustomerDocument();

                                currDocument.setDlNo(edtDlNo.getText().toString());




                                currDocument.setDlFront(uri.toString());

                                doc.child(mAuth.getUid()).setValue(currDocument);


                            }
                        });

                    }
                }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                mDialog.dismiss();
                Toast.makeText(UserDocument.this,""+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());
                mDialog.setMessage("Uploaded "+progress+"%");


            }
        });

    }
}

0 个答案:

没有答案