Skip to content

Instantly share code, notes, and snippets.

@RaheemJnr
Last active October 1, 2022 10:07
Show Gist options
  • Save RaheemJnr/8c3eefa4f7343e475874482a271a3382 to your computer and use it in GitHub Desktop.
Save RaheemJnr/8c3eefa4f7343e475874482a271a3382 to your computer and use it in GitHub Desktop.
//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