将多张图片上传到 Firebase 并检索到 viewpage

Posted

技术标签:

【中文标题】将多张图片上传到 Firebase 并检索到 viewpage【英文标题】:Upload multiple image to firebase and retrieve into viewpage 【发布时间】:2020-10-18 11:34:29 【问题描述】:

我想将多张图片上传到 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);

// 文件 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】:

试试这个方法……

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

【讨论】:

以上是关于将多张图片上传到 Firebase 并检索到 viewpage的主要内容,如果未能解决你的问题,请参考以下文章

如何将多个图像上传到 Firebase 存储并返回多个下载 URL

我正在将多张图片上传到 Firebase 并已完成,但我无法将 url 链接保存到列表中

使用 Google Firebase 同时上传多张图片

将多个图像的异步图像上传到firebase存储

Flutter Firebase 上传多张图片

Flutter 无法上传从 multi_image_picker 获取的多张图片