How to upload an image to Firebase storage?

前端 未结 5 1309
独厮守ぢ
独厮守ぢ 2021-01-04 07:55

I\'m trying to upload a simple byte array into Firebase storage, but my onFailureListener keeps getting called and logging back to me saying that the upload fai

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-04 08:42

    Upload multilple images to Firebase storage.

    It is working for me.

    using this library

      compile 'com.github.darsh2:MultipleImageSelect:3474549'
    

    At the top

    private StorageReference storageRef;
    private FirebaseApp app;
    private FirebaseStorage storage;
    

    onCreate()

    app = FirebaseApp.getInstance();
    storage =FirebaseStorage.getInstance(app);
    

    button click action

       Gallary.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
    
                    Intent intent = new Intent(ChatActivity.this, AlbumSelectActivity.class);
                    intent.putExtra(Constants.INTENT_EXTRA_LIMIT, 10);
                    startActivityForResult(intent, Constants.REQUEST_CODE);
    
                }
            });
    

    Activity result

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constants.REQUEST_CODE && resultCode == RESULT_OK) {
            ArrayList images = data.getParcelableArrayListExtra(Constants.INTENT_EXTRA_IMAGES);
            Uri[] uri=new Uri[images.size()];
            for (int i =0 ; i < images.size(); i++) {
                uri[i] = Uri.parse("file://"+images.get(i).path);
                StorageReference ref = storage.getReference("photos").child(uri[i].getLastPathSegment());
                ref.putFile(uri[i])
                        .addOnSuccessListener(new OnSuccessListener() {
                            @Override
                            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                                Uri downloadUrl = taskSnapshot.getDownloadUrl();
                                String content = downloadUrl.toString();
    
                            }
                        })
                        .addOnFailureListener(new OnFailureListener() {
                            @Override
                            public void onFailure(@NonNull Exception exception) {
    
                            }
                        })
                        .addOnProgressListener(new OnProgressListener() {
                            @Override
                            public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                                //displaying the upload progress
                                double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
                                progressDialog.setMessage("Uploaded " + ((int) progress) + "%...");
                            }
                        });
    
              }
            }
    
        }
    

提交回复
热议问题