1

I'm trying to upload a picture to firebase. If I load the picture from an InputStream to my app and upload it with putBytes(), it works, if I try to directly upload the picture with putStream(), I get an error message, that the stream is closed:

11-09 14:16:39.126 26077-26586/com.finder E/StorageException: StorageException has occurred.
    An unknown error occurred, please check the HTTP result code and inner exception for server response.
     Code: -13000 HttpResult: 200
11-09 14:16:39.126 26077-26586/com.finder E/StorageException: closed
    java.io.IOException: closed
        at com.android.okhttp.okio.RealBufferedSource$1.read(RealBufferedSource.java:366)
        at com.google.firebase.storage.internal.AdaptiveStreamBuffer.fill(com.google.firebase:firebase-storage@@16.0.4:107)
        at com.google.firebase.storage.UploadTask.uploadChunk(com.google.firebase:firebase-storage@@16.0.4:396)
        at com.google.firebase.storage.UploadTask.run(com.google.firebase:firebase-storage@@16.0.4:205)
        at com.google.firebase.storage.StorageTask.lambda$getRunnable$7(com.google.firebase:firebase-storage@@16.0.4:1106)
        at com.google.firebase.storage.StorageTask$$Lambda$12.run(com.google.firebase:firebase-storage@@16.0.4)
        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)

The method, that uploads the picture:

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 = filePathRoot.child(id);


                try (InputStream is = new URL(url).openStream()) { StorageMetadata.Builder().setContentType("image/jpg").setContentLanguage("en").build();


                UploadTask uploadTask = filePath.putStream(is);

                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) {
                Log.e(TAG, "BufferedInputStream DIIIIID throw MalformedURLException 2 " + e.getMessage());
            } catch (IOException e) {
                Log.e(TAG, "BufferedInputStream DIIIIID throw IOException 2 " + e.getMessage());
            } catch (Exception e) {
            Log.e(TAG, "BufferedInputStream DIIIIID throw Exception 2 " + e.getClass() + " " + e.getMessage());
        }

            return null;
        }
    }

Doesn't matter if I use an InputStream or a BufferedInputStream.

Resuming UploadTask results in E/StorageException: BufferedInputStream is closed says to use another version of Firebase, but since it is an old post, I already am.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
murkr
  • 634
  • 5
  • 27

0 Answers0