如何将多个图像上传到firebase并在单独的子项中获取URL只是获取一个子值中的所有URL

时间:2017-11-14 23:30:40

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

public class MainActivity extends AppCompatActivity {

    String key;
    private ImageView mImageView;
    private FirebaseStorage mStorageRef;
    private Button mButton;
    private StorageTask mUploadTask;
    private List<Uri> uploadedImages = new ArrayList<>();

    private DatabaseReference mRefre;
    ArrayList<String> filepath = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mImageView = (ImageView) findViewById(R.id.hhhh);

        mRefre = FirebaseDatabase.getInstance().getReference();
        mButton = (Button) findViewById(R.id.addphoto);

        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                filepath.clear();

                FilePickerBuilder.getInstance().setMaxCount(3)
                        .setSelectedFiles(filepath)
                        .setActivityTheme(R.style.AppTheme)
                        .pickPhoto(MainActivity.this);    
            }
        });
    }

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

            case FilePickerConst.REQUEST_CODE:

                if (resultCode == RESULT_OK && data != null) {

                    final ArrayList<String> filepath;
                    filepath = data.getStringArrayListExtra(FilePickerConst.KEY_SELECTED_PHOTOS);

                    filepath.add("image1");
                    filepath.add("image2");

                    for (String path : filepath) {

                        final Uri file = Uri.fromFile(new File(path));
                        key = mRefre.child("products").push().getKey();

                        StorageReference photoRef = FirebaseStorage.getInstance().getReference("Images").child(uploadedImages + file.getLastPathSegment());

                        mUploadTask = photoRef.putFile(file);
                        mUploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                            @Override
                            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                                Uri downloadUrl = taskSnapshot.getDownloadUrl();
                                uploadedImages.add(downloadUrl);

                                HashMap userMap = new HashMap();
                                userMap.put("image1",uploadedImages.toString());
                                userMap.put("image2",uploadedImages.toString());
                                mRefre.child("product-images").setValue(userMap);
                            }
                        });    
                    }
                }
        }
    }
}

我正在进行firebase项目并尝试使用最多3张图片的imagepicker上传多张图片。图像已成功上传到firebase存储,但是当我尝试获取URL时,它会将3个图像保存在一个子值中。我试图将它保存在单独的值中并命名它们以便我以后可以检索它们。

我该如何解决这个问题?

Image

1 个答案:

答案 0 :(得分:0)

我尝试了一种不同的方法:使用内置的Promise / Task构造而不是循环。详情请参阅:https://gist.github.com/mcdonamp/d4f1327b58ad69334ef06327184df790How to upload multiple files to Firebase?Android Firebase multiple image upload