Samseen
Samseen

Reputation: 97

How do I upload a converted drawable in android to Firebase?

In my application, I am able to properly convert the drawable to an image and save it locally on the device. However, I'll need to push the image to Firebase and not save it locally. Here's what I have done.

public void saveImage () {

    int count = 0;

    File sdDirectory = Environment.getExternalStorageDirectory();
    File subDirectory = new File(sdDirectory.toString() + "/Pictures/Paint");

    if (subDirectory.exists()) {
        File[] existing = subDirectory.listFiles();

        for (File file : existing) {
            if (file.getName().endsWith(".jpg") || file.getName().endsWith(".png")) {

                count++;
            }
        }
    } else {
        subDirectory.mkdir();
    }

    if (subDirectory.exists()) {
        File image = new File(subDirectory, "/drawing_" + (count + 1) + ".png");

        FileOutputStream fileOutputStream;

        try {
            fileOutputStream = new FileOutputStream(image);
            //The code below still doesn't save the image online in firebase.
            Uri file = Uri.fromFile(image);
//            StorageReference fileRef = reference.child(System.currentTimeMillis() + "." + getFileExtension(file));
            StorageReference fileRef = reference.child(System.currentTimeMillis() + ".png");

            fileRef.putFile(file).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    fileRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                        @Override
                        public void onSuccess(Uri uri) {
                            Model model = new Model(uri.toString());
                    String modelId = root.push().getKey();
                    root.child(modelId).setValue(model);
                    progressBar.setVisibility(View.INVISIBLE);
                            Toast.makeText(getContext(), "Uploaded Successfully!", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Toast.makeText(getContext(), "Uploading Failed!", Toast.LENGTH_SHORT).show();
                }
            });

            Boolean bool = mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);

            fileOutputStream.flush();
            fileOutputStream.close();

            Toast.makeText(getContext(), "saved locally", Toast.LENGTH_LONG).show();
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        }
    }
}

It's always failing to upload the file but it successfully saves it locally. I've even just tried to display a notification upon a successful upload but it doesn't even get to the addOnSuccessListener talk less of the onSuccessMethod. I feel like I'm close but what could I be doing wrong?

Upvotes: 0

Views: 92

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599166

Your code is ignoring all kinds of error, which make it really hard for you (let alone us) to troubleshoot.

First off: stop catching errors and doing nothing with them, like in } catch (FileNotFoundException e) {. At the very least you should log them with:

Log.e("Storage", "Error uploading image", e)

Same for the IOException, and for public void onFailure(@NonNull Exception e) {: if you log e there too, you may be able to find (in your logcat output) what is going wrong.

Upvotes: 1

Related Questions