android: upload of pictures from facebook to fireabse returns 404

So, I code an android app, that takes pictures from facebook and stores them in a firebase db.

I use the graph-api to get the urls of the pictures, which works fine, since I can put the urls in a browser and the pictures appear.

Now I want to use an UploadTask to upload these pictures to firebase, and I get:

E/StorageException: StorageException has occurred.
    Object does not exist at location.
     Code: -13010 HttpResult: 404
11-07 19:58:00.894 8612-9182/com.finder E/StorageException: {  "error": {    "code": 404,    "message": "Not Found.  Could not get object"  }}
    java.io.IOException: {  "error": {    "code": 404,    "message": "Not Found.  Could not get object"  }}
        at com.google.firebase.storage.network.NetworkRequest.parseResponse(com.google.firebase:firebase-storage@@16.0.4:455)
        at com.google.firebase.storage.network.NetworkRequest.parseErrorResponse(com.google.firebase:firebase-storage@@16.0.4:435)
        at com.google.firebase.storage.network.NetworkRequest.processResponseStream(com.google.firebase:firebase-storage@@16.0.4:426)
        at com.google.firebase.storage.network.NetworkRequest.performRequest(com.google.firebase:firebase-storage@@16.0.4:280)
        at com.google.firebase.storage.network.NetworkRequest.performRequest(com.google.firebase:firebase-storage@@16.0.4:294)
        at com.google.firebase.storage.internal.ExponentialBackoffSender.sendWithExponentialBackoff(com.google.firebase:firebase-storage@@16.0.4:65)
        at com.google.firebase.storage.internal.ExponentialBackoffSender.sendWithExponentialBackoff(com.google.firebase:firebase-storage@@16.0.4:57)
        at com.google.firebase.storage.GetDownloadUrlTask.run(com.google.firebase:firebase-storage@@16.0.4:71)
        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) 

Here is my upload method:

static class UploadPicAsyncWrapperTask extends AsyncTask<String, Void, Void> {
            @Override
            protected Void doInBackground(String... strings) {

                String picsFolder = strings[0];
                String id = strings[1];
                final String url = strings[2];
                final String profilepicNodeName = strings[3];
                final String picsNodeName = strings[4];

                final StorageReference filePathRoot = FirebaseStorage.getInstance().getReference().child(picsFolder).child(FirebaseAuth.getInstance().getCurrentUser().getUid());
                final StorageReference filePath;

                filePath = filePathRoot.child(id);

                filePath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                    @Override
                    public void onSuccess(Uri uri) {
                        // File already exists
                    }
                }).addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception exception) {

                        final UploadTask uploadTask;

                        try (InputStream is = new URL(Uri.encode(url)).openStream()) {
                            uploadTask = filePath.putStream(is); //Uri.fromFile(new File(url));  //Uri.parse(url) new ByteArrayInputStream()

                            uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                                @Override
                                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                                    filePath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                                        @Override
                                        public void onSuccess(Uri uri) {
                                            if (profilePicName == null) {
                                                mUserDatabase.child(profilepicNodeName).setValue(uri.toString());
                                                profilePicName = uri.toString();

                                            } else {
                                                mUserDatabase.child(picsNodeName).child(Helper.imageUrlToFirebasePath(uri.toString())).setValue(uri.toString());
                                            }
                                            --picsRemaining;
                                        }
                                    });
                                }
                            });
                        } catch (MalformedURLException e) {
                            // TODO or not todo whatever
                        } catch (IOException e) {
                            // TODO or not todo whatever
                        }
                    }
                });

                return null;
            }
        }

And this is how the asyncTask is called:

new UploadPicAsyncWrapperTask().execute(getString(R.string.firebase_storage_pics_folder), id, url, getString(R.string.firebase_database_profilepic_node_name), getString(R.string.firebase_database_pics_node_name));

Anybody any idea why firebase can't catch the pictures?

1 answer

  • answered 2018-11-08 14:39 murkr

    So, there is nothing wrong with my code above. Since I can't ask Firebase if a picture was already uploaded to the database, I ask firebase to give me the downloadUrl of that picture and in onFailureListener, I upload the picture.

    What I didn't know is, that onFailureListener implicit logs the error message above.