Skip to content

Instantly share code, notes, and snippets.

@jonathanbcsouza
Last active October 5, 2021 05:47
Show Gist options
  • Save jonathanbcsouza/13929ab81077645f1033bf9ce45beaab to your computer and use it in GitHub Desktop.
Save jonathanbcsouza/13929ab81077645f1033bf9ce45beaab to your computer and use it in GitHub Desktop.
SOLUTION FOR: DEPRECATED >> Uri downloadUrl = taskSnapshot.getDownloadUrl
// Udacity - Does not working
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
if (resultCode == RESULT_OK) {
Toast.makeText(getContext(), "Signed in!", Toast.LENGTH_SHORT).show();
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(getContext(), "Sign in canceled", Toast.LENGTH_SHORT).show();
getActivity().finish();
} else if (requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK) {
Uri selectedImageUri = data.getData();
// Get a reference to store file at chat_photos/<FILENAME>
StorageReference photoRef = mChatPhotosStorageReference.child(selectedImageUri.getLastPathSegment());
mChatPhotosStorageReference.child(selectedImageUri.getLastPathSegment());
// Upload file to Firebase Storage
photoRef.putFile(selectedImageUri)
.addOnSuccessListener(this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// When the image has successfully uploaded, we get its download URL
Uri downloadUrl = taskSnapshot.getDownloadUrl();
// Set the download URL to the message box, so that the user can send it to the database
ChatMessageClass friendlyMessage = new ChatMessageClass(null, mUsername, downloadUrl.toString());
mMessagesDatabaseReference.push().setValue(friendlyMessage);
}
});
}
}
}
// ------------------------------------------------------------------------------------------------------------------------------------
//Update - SOLUTION
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN && resultCode == RESULT_OK) {
Toast.makeText(this, "Signed in!", Toast.LENGTH_SHORT).show();
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Sign in canceled!", Toast.LENGTH_SHORT).show();
} else if (requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK) {
Uri selectedImageUri = data.getData();
// Get a reference to store file at chat_photos/<FILENAME>
final StorageReference photoRef = mChatPhotosStorageReference.child(selectedImageUri.getLastPathSegment());
photoRef.putFile(selectedImageUri)
.addOnSuccessListener(this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
//When the image has successfully uploaded, get its download URL
photoRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
Uri dlUri = uri;
FriendlyMessage friendlyMessage = new FriendlyMessage(null, mUsername, dlUri.toString());
mMessagesDatabaseReference.push().setValue(friendlyMessage);
}
});
}
});
}
}
@theBlackCrow34
Copy link

thank you very much

@jerrychong25
Copy link

Thanks, this is working!

@Mridul0001
Copy link

Thanks, you made my day.

@sridattayalla
Copy link

Thank you, this is really helpful

@oscarcelis6
Copy link

Muy útil, muchas gracias.

@maheensaleh
Copy link

thanks]

@Soufiane-Aou
Copy link

thanks a looooooot

@jonathanbcsouza
Copy link
Author

jonathanbcsouza commented Apr 30, 2020

I know what kind of feeling you might have had to get this sorted 😅.
I'm glad it helped.
Cheers.

@abhigitzone
Copy link

Man... you don't know how much i am loving you right now. You just made my day.
Thank you so much.

@abhigitzone
Copy link

There is one minor bug,

  1. when you clicked on the photo picker button but press back button, a Toast appear with sign in cancelled.
  2. When you logged out of the app, and then press back button , it keep moving in loops.
    --------SOLUTION---------
    Change the First : else if statements to this: by inserting requestCode ==RC_SIGN_IN to field and finish in the last.
    else if (requestCode == RC_SIGN_IN && resultCode == RESULT_CANCELED) { Toast.makeText(this, "Sign in canceled!", Toast.LENGTH_SHORT).show(); finish();

@samsofa
Copy link

samsofa commented Jul 4, 2020

thank you

@aashutosh-rana
Copy link

Man... you don't know how much i am loving you right now. You just made my day.
Thank you so much.

i think you should get married with him. by the way this man also solve my prob.

@hammadkhan
Copy link

It finally solves the problem, but I still face an issue. The image is not shown right away but is shown when the app is loaded again. What is causing this, how can that be fixed?

@ahmednasserzaza
Copy link

thanks

@momoali19
Copy link

Thank you very much man, you saved my day

@AkinyiFO
Copy link

Muchas gracias!!!

@Sandydalvi2001
Copy link

Thanks man

@zufa1984
Copy link

zufa1984 commented Jul 8, 2021

thank you man

@priyankaaa532002
Copy link

THANKS A LOT MAYN!!!!!! FINALLLLLYYYYYY WORKING. YOU ARE THE BEST PERSON ALIVE😭😭😭😭💖💖

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment