Last active
February 17, 2020 04:37
-
-
Save john-lorrenz/1d9e51c887429ee24f1edab8279b381b to your computer and use it in GitHub Desktop.
Get Photo From Gallery / Capture from Camera
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
implementation 'com.github.florent37:runtime-permission:1.1.1' |
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
import android.Manifest | |
import com.github.florent37.runtimepermission.RuntimePermission | |
import android.provider.MediaStore | |
import java.util.* | |
import java.io.File | |
import android.net.Uri | |
import android.content.Intent | |
var imageUri: Uri? = null | |
var OPEN_CAMERA = 10 | |
var OPEN_GALLERY = 11 | |
button.setOnClickListener { | |
askUploadSource() | |
} | |
private fun askUploadSource(){ | |
val options = arrayOf("Open Gallery", "Capture Photo") | |
val builder = android.app.AlertDialog.Builder(this) | |
builder.setTitle("Please pick your image source") | |
builder.setItems(options){ _, which -> | |
if(which == 0){ | |
// ASK PERMISSION TO OPEN GALLERY | |
RuntimePermission.askPermission(this) | |
.request(Manifest.permission.READ_EXTERNAL_STORAGE) | |
.onAccepted{ | |
// OPEN GALLERY | |
openGallery() | |
} | |
.ask() | |
}else if(which == 1){ | |
// ASK PERMISSION TO OPEN CAMERA | |
RuntimePermission.askPermission(this) | |
.request(Manifest.permission.CAMERA) | |
.onAccepted{ | |
// OPEN CAMERA | |
openCamera() | |
} | |
.ask() | |
} | |
} | |
builder.show() | |
} | |
fun openCamera(){ | |
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE); | |
var filename = UUID.randomUUID().toString() + ".jpg" | |
var file = File(this.externalCacheDir, filename) | |
imageUri = Uri.fromFile(file) | |
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri) | |
startActivityForResult(intent, OPEN_CAMERA) | |
} | |
fun openGallery(){ | |
val intent = Intent(Intent.ACTION_PICK) | |
intent.type = "image/*" | |
startActivityForResult(intent, OPEN_GALLERY) | |
} | |
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { | |
super.onActivityResult(requestCode, resultCode, data) | |
if (requestCode == OPEN_GALLERY && data!= null) { imageUri = data!!.data } | |
if (resultCode == RESULT_OK && imageUri != null) { | |
// do something with the image here | |
// example: imageView.setImageURI(imageUri) | |
} | |
} |
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
fun uploadImageToFirebaseStorage(imageUri: Uri, callback: (url: String) -> Unit) { | |
var filename = UUID.randomUUID().toString() | |
var storageRef = FirebaseStorage.getInstance().getReference("/user_uploads/$filename.jpg") | |
// UPLOAD TO FIREBASE | |
storageRef.putFile(imageUri) | |
.addOnSuccessListener { | |
storageRef.downloadUrl.addOnSuccessListener { | |
var url = it.toString() | |
callback(url) | |
} | |
} | |
.addOnFailureListener{ | |
println( it.toString()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment