Last active
April 17, 2019 04:58
-
-
Save jirawatee/606ca9b2729f832b54cf2074c708a376 to your computer and use it in GitHub Desktop.
Firebase Storage - UploadFromDataInMemory.java
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
private void uploadFromDataInMemory() { | |
Helper.showDialog(this); | |
// Get the data from an ImageView as bytes | |
mImageView.setDrawingCacheEnabled(true); | |
mImageView.buildDrawingCache(); | |
Bitmap bitmap = mImageView.getDrawingCache(); | |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); | |
byte[] data = baos.toByteArray(); | |
mUploadTask = imageRef.putBytes(data); | |
mUploadTask.addOnFailureListener(new OnFailureListener() { | |
@Override | |
public void onFailure(@NonNull Exception exception) { | |
Helper.dismissDialog(); | |
mTextView.setText(String.format("Failure: %s", exception.getMessage())); | |
} | |
}).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. | |
Helper.dismissDialog(); | |
imageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() { | |
@Override | |
public void onSuccess(Uri uri) { | |
mTextView.setText(uri.toString()); | |
} | |
}); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment