Last active
October 1, 2022 10:07
-
-
Save RaheemJnr/8c3eefa4f7343e475874482a271a3382 to your computer and use it in GitHub Desktop.
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
//the upload method | |
fun uploadPhoto( | |
context: Context, | |
imageUri: Uri, | |
contentResolver: ContentResolver, | |
fileKey: String | |
): String { | |
var url = "" | |
val stream = contentResolver.openInputStream(imageUri) | |
if (stream != null) { | |
Amplify.Storage.uploadInputStream( | |
fileKey, | |
stream, | |
{ message -> | |
showToast(context = context, "Upload Successful:$message", Toast.LENGTH_SHORT) | |
Log.e("Amplify", "Upload Successful ${message.key}") | |
val uploadedFile = getUploadedFileUrl(fileKey) | |
url = uploadedFile | |
}, | |
{ error -> | |
showToast(context = context, "Upload failed,", Toast.LENGTH_SHORT) | |
Log.e("Amplify", "Upload Failed", error) | |
} | |
) | |
} | |
return url | |
} | |
//the call site | |
//the image uri can be gotten from the phone camera or the gallery | |
uploadPhoto( | |
context = context, | |
imageUri = imageUri, | |
contentResolver = context.contentResolver, | |
fileKey = "${localUserId}${getCurrentTimeMillis()}.jpg" | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment