将多张图片上传到Firebase并检索到视图页面

时间:2020-06-28 06:13:49

标签: java android database firebase firebase-realtime-database

我想将多个图像上传到Firebase并检索到视图页面中。我只能上传多张图片,但在实时数据库中仅获得一个Uri getdlownload链接,因为该链接仅将一张图片提取到视图页面中

enter code here
private void sendDataToFirebase() {
    progressBar.setVisibility(View.VISIBLE);
    id = databaseReference.push().getKey();

    Toast.makeText(this, "Sending", Toast.LENGTH_SHORT).show();



   photoUri=new Uri[photoUris.size()];
    for (i = 0; i < photoUris.size(); i++) {
       photoUri[i] = photoUris.get(i);

// File fileUriPhoto = new File(SiliCompressor.with(this)。 // // compress(FileUtils.getPath(this,photoUri),new File(this.getCacheDir(),“ AddPostPhotos”)))); // Uri uri = Uri.fromFile(fileUriPhoto);

        String imageName = UUID.randomUUID().toString();
        final StorageReference imageFolder = mfirebaseStorage.child("AddPost/" + photoUri[i].getLastPathSegment());


            imageFolder.putFile(photoUri[i]).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                    imageFolder.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                        @Override
                        public void onSuccess(Uri uri) {
                            progressBar.setVisibility(View.GONE);

                            addPostModel = new AddPostModel();
                            addPostModel.setCategories(spinner.getSelectedItem().toString().trim());
                            addPostModel.setSubcategories(spinner1.getSelectedItem().toString().trim());
                            addPostModel.setTitle(name.getText().toString().trim());
                            addPostModel.setAuthor(author.getText().toString().trim());
                            addPostModel.setPrice(price.getText().toString().trim());
                            addPostModel.setDesc(desc.getText().toString().trim());
                            addPostModel.setUserUid(mCurrentUser.getUid());
                            addPostModel.setPhoneNumaber(mCurrentUser.getPhoneNumber());
                            addPostModel.setDateTime(date);
                            addPostModel.setImageUrl(photoUri[i].toString());//Here is the problem//
                            addPostModel.setKey(id);
                            databaseReference.child(id).setValue(addPostModel);



                        }
                    });

                }
            }).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {

                    Toast.makeText(AddPostFormActivity.this, "Post added", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(AddPostFormActivity.this,Dashboard.class);
                    startActivity(intent);

                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Toast.makeText(AddPostFormActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
        }

1 个答案:

答案 0 :(得分:0)

尝试此方法...

imageFolder.putFile(photoUri[i]).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                //Here you are get the download url 
                Task<Uri> urlTask =taskSnapshot.getStorage().getDownloadUrl();
                Uri downloadUrl = urlTask.getResult();
                final String sdownload_url = String.valueOf(downloadUrl);
                
                
                progressBar.setVisibility(View.GONE);
                addPostModel = new AddPostModel();
                        
              addPostModel.setCategories(spinner.getSelectedItem().toString().trim());
                        addPostModel.setSubcategories(spinner1.getSelectedItem().toString().trim());
                        addPostModel.setTitle(name.getText().toString().trim());
                        addPostModel.setAuthor(author.getText().toString().trim());
                        addPostModel.setPrice(price.getText().toString().trim());
                        addPostModel.setDesc(desc.getText().toString().trim());
                        addPostModel.setUserUid(mCurrentUser.getUid());
                        addPostModel.setPhoneNumaber(mCurrentUser.getPhoneNumber());
                        addPostModel.setDateTime(date);
                        addPostModel.setImageUrl(photoUri[i].toString());//Here is the problem//
                        addPostModel.setKey(id);
                        databaseReference.child(id).setValue(addPostModel);

            }
        }).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {

                Toast.makeText(AddPostFormActivity.this, "Post added", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(AddPostFormActivity.this,Dashboard.class);
                startActivity(intent);

            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(AddPostFormActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });