Last active
June 15, 2019 18:34
-
-
Save nglauber/5aa7ed41da8264881b7c40f5e8ab7a60 to your computer and use it in GitHub Desktop.
Upload image using firebase
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void uploadImage(Bitmap bitmap) { | |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); | |
byte[] data = baos.toByteArray(); | |
FirebaseStorage storage = FirebaseStorage.getInstance(); | |
StorageReference storageRef = storage.getReferenceFromUrl("gs://you_firebase_app.appspot.com"); | |
StorageReference imagesRef = storageRef.child("images/name_of_your_image.jpg"); | |
UploadTask uploadTask = imagesRef.putBytes(data); | |
uploadTask.addOnFailureListener(new OnFailureListener() { | |
@Override | |
public void onFailure(@NonNull Exception exception) { | |
// Handle unsuccessful uploads | |
} | |
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() { | |
@Override | |
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { | |
// taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL. | |
Uri downloadUrl = taskSnapshot.getDownloadUrl(); | |
// Do what you want | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment