Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
//upload image for firebase storage
private void updateImage(ImageView imageView){
// Create a storage reference from our app
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference storageRef = storage.getReferenceFromUrl("<your-storage-url>");
String uuid = UUID.randomUUID().toString();
StorageReference mountainsRef = storageRef.child(uuid + ".jpg");
Bitmap bitmap = imageView.getDrawingCache();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] data = baos.toByteArray();
UploadTask uploadTask = mountainsRef.putBytes(data);
uploadTask.addOnFailureListener(new OnFailureListener() {
public void onFailure(@NonNull Exception exception) {
// Handle unsuccessful uploads
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
Uri downloadUrl = taskSnapshot.getDownloadUrl();
Log.e("info", downloadUrl.toString());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment