Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
638 views
in Technique[技术] by (71.8m points)

android - How to upload an image to Firebase storage?

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 failed. I'm hoping you guys can tell me whats wrong with my code.

At the top I got

 //Firebase
    private Firebase mRef;
    private StorageReference storageRef;

Then in onStart()

@Override
protected void onStart() {
    super.onStart();
    googleApiClient.connect();
    //Firebase
    mRef = new Firebase("link to firebase account");
    FirebaseStorage storage = FirebaseStorage.getInstance();
    storageRef = storage.getReferenceFromUrl("link to storage");

}

Then in my onActivityResult()

      @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            addImageImageView.setVisibility(View.GONE);
            if (requestCode == Constants.REQUEST_CODE && resultCode == RESULT_OK && data != null) {
                //First we gotta make sure to add the images to
                ArrayList<Image> imagesFromGallery = data.getParcelableArrayListExtra(Constants.INTENT_EXTRA_IMAGES);
                for (int i = 0; i < imagesFromGallery.size(); i++) 
                {
                    try {
                        //try uploading it
                        InputStream stream = new FileInputStream(new File(imagesFromGallery.get(i).path));
                        StorageReference imageStorage = storageRef.child("cardImages/" + "testImages");
                        UploadTask uploadTask = imageStorage.putStream(stream);
                        uploadTask.addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Log.d("myStorage","failure :(");
                        }
                    }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                            Log.d("myStorage","success!");
                        }
                    });
                    catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                    } catch (FileNotFoundException e) {
                     e.printStackTrace();
                }
           }
      }

Here is my stack trace:

11-13 21:27:00.392 31388-996/com.daprlabs.aaron.swipedeck2 E/StorageException: StorageException has occurred.
   User does not have permission to access this object.
    Code: -13021 HttpResult: 403
11-13 21:27:00.392 31388-996/com.daprlabs.aaron.swipedeck2 E/StorageException: The server has terminated the upload session
   java.io.IOException: The server has terminated the upload session
       at com.google.firebase.storage.UploadTask.az(Unknown Source)
       at com.google.firebase.storage.UploadTask.ay(Unknown Source)
       at com.google.firebase.storage.UploadTask.run(Unknown Source)
       at com.google.firebase.storage.StorageTask$5.run(Unknown Source)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
       at java.lang.Thread.run(Thread.java:818)
11-13 21:27:00.406 31388-31388/com.daprlabs.aaron.swipedeck2 D/myStorage: failure :(
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...