Skip to content

Instantly share code, notes, and snippets.

View jirawatee's full-sized avatar
🔥
Better Together

Jirawat Karanwittayakarn jirawatee

🔥
Better Together
View GitHub Profile
@jirawatee
jirawatee / UploadFromLocalFile.java
Last active April 17, 2019 05:01
Firebase Storage - Upload from local file
private void uploadFromFile(String path) {
Helper.showDialog(this);
Uri file = Uri.fromFile(new File(path));
StorageReference imageRef = folderRef.child(file.getLastPathSegment());
mUploadTask = imageRef.putFile(file);
mUploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
Helper.dismissDialog();
@jirawatee
jirawatee / UploadFromLocalFileFull.java
Last active April 17, 2019 05:02
Firebase Storage - Upload from local file (Full)
private void uploadFromFile(String path) {
Uri file = Uri.fromFile(new File(path));
StorageReference imageRef = folderRef.child(file.getLastPathSegment());
StorageMetadata metadata = new StorageMetadata.Builder()
.setContentType("image/jpg")
.setCustomMetadata("country", "Thailand"
.build();
UploadTask uploadTask = imageRef.putFile(file, metadata);
Helper.initProgressDialog(this);
@jirawatee
jirawatee / DownloadInMemory.java
Last active April 17, 2019 05:03
Firebase Storage - Download in memory
private void downloadInMemory() {
//long ONE_MEGABYTE = 1024 * 1024;
Helper.showDialog(this);
imageRef.getBytes(Long.MAX_VALUE).addOnSuccessListener(new OnSuccessListener<byte[]>() {
@Override
public void onSuccess(byte[] bytes) {
Helper.dismissDialog();
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
mImageView.setImageBitmap(bitmap);
}
@jirawatee
jirawatee / DownloadInLocalFile.java
Last active April 17, 2019 05:48
Firebase Storage - Download in local file
private void downloadInLocalFile() {
File dir = new File(Environment.getExternalStorageDirectory() + "/photos");
final File file = new File(dir, UUID.randomUUID().toString() + ".png");
try {
if (!dir.exists()) {
dir.mkdir();
}
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
@jirawatee
jirawatee / DownloadDataViaUrl.java
Last active April 17, 2019 05:47
Firebase Storage - Download data via url
private void downloadDataViaUrl() {
Helper.showDialog(this);
imageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
Helper.dismissDialog();
mTextView.setText(uri.toString());
}
}).addOnFailureListener(new OnFailureListener() {
@Override
@jirawatee
jirawatee / DeleteFile.java
Last active April 17, 2019 05:48
Firebase Storage - Delete file
private void deleteFile() {
Helper.showDialog(this);
imageRef.delete().addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Helper.dismissDialog();
mImageView.setImageDrawable(null);
mTextView.setText(String.format("%s was deleted.", imageRef.getPath()));
}
}).addOnFailureListener(new OnFailureListener() {
@jirawatee
jirawatee / GetMatadata.java
Last active April 17, 2019 05:49
Firebase Storage - Get metadata
private void getMetadata() {
Helper.showDialog(this);
imageRef.getMetadata().addOnSuccessListener(new OnSuccessListener<StorageMetadata>() {
@Override
public void onSuccess(StorageMetadata storageMetadata) {
Helper.dismissDialog();
mTextView.setText(String.format("Country: %s", storageMetadata.getCustomMetadata("country")));
}
}).addOnFailureListener(new OnFailureListener() {
@Override
@jirawatee
jirawatee / UpdateFileMetadata.java
Last active April 17, 2019 05:49
Firebase Storage - Update file metadata
private void updateMetaData() {
Helper.showDialog(this);
StorageMetadata metadata = new StorageMetadata.Builder().setCustomMetadata("country", "Thailand").build();
imageRef.updateMetadata(metadata).addOnSuccessListener(new OnSuccessListener<StorageMetadata>() {
@Override
public void onSuccess(StorageMetadata storageMetadata) {
Helper.dismissDialog();
mTextView.setText(String.format("Country: %s", storageMetadata.getCustomMetadata("country")));
}
}).addOnFailureListener(new OnFailureListener() {
@jirawatee
jirawatee / MainActivity.java
Last active September 29, 2017 12:08
Firebase Invites - Send invitation via email
Intent intent = new AppInviteInvitation.IntentBuilder("Send App Invitation")
.setMessage("Special season for this app is starting now, try it and get 100 baht")
.setDeepLink(Uri.parse("http://example.com/offer/100_baht"))
.setCustomImage(Uri.parse("https://appjoy.org/wp-content/uploads/2016/06/firebase-invites-logo.png"))
.setCallToActionText("Install")
.build();
startActivityForResult(intent, REQUEST_INVITE);
@jirawatee
jirawatee / MainActivity.java
Created September 29, 2017 07:47
Firebase Invites - Send invitation via SMS and Email
Intent intent = new AppInviteInvitation.IntentBuilder("Send App Invitation")
.setMessage("Special season for this app is starting now, try it and get 100 baht")
.setDeepLink(Uri.parse("http://example.com/offer/100_baht"))
.build();
startActivityForResult(intent, REQUEST_INVITE);