I need to take an image and then save it in a location, after i need to add that location to a room database, which i then use to display all the photos i have taken . Want to make a contact list where i can take picture of a person and add them to a list thats displayed with recycler view. I have setup a room database and managed to save it in pictures. But i have been unable to retrieve the file and display it in a imageview(to test i i could retrieve it).
i use this code to take a picture and save it.
private fun openCameraInterface() {
val values = ContentValues()
println(ContentValues())
values.put(MediaStore.Images.Media.TITLE, "win")
values.put(MediaStore.Images.Media.DISPLAY_NAME, "player")
values.put(MediaStore.Images.Media.DESCRIPTION, "")
values.put(MediaStore.Images.Media.MIME_TYPE, "image/" + "jpeg");
imageUri = this.contentResolver?.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)
// Create camera intent
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri)
// Launch intent
startActivityForResult(intent, IMAGE_CAPTURE_CODE)
}
this is the rest of the camera code
private fun requestCameraPermission(): Boolean {
var permissionGranted = false
// If system os is Marshmallow or Above, we need to request runtime permission
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
val cameraPermissionNotGranted = checkSelfPermission(this as Context, Manifest.permission.CAMERA) == PackageManager.PERMISSION_DENIED
if (cameraPermissionNotGranted){
val permission = arrayOf(Manifest.permission.CAMERA)
// Display permission dialog
requestPermissions(permission, CAMERA_PERMISSION_CODE)
}
else{
// Permission already granted
permissionGranted = true
}
}
else{
// Android version earlier than M -> no need to request permission
permissionGranted = true
}
return permissionGranted
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode === CAMERA_PERMISSION_CODE) {
if (grantResults.size === 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
// Permission was granted
openCameraInterface()
}
else{
// Permission was denied
showAlert("Camera permission was denied. Unable to take a picture.");
}
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
// Callback from camera intent
if (resultCode == Activity.RESULT_OK){
// Set image captured to image view
}
else {
// Failed to take picture
showAlert("Failed to take camera picture")
}
}
private fun showAlert(message: String) {
val builder = AlertDialog.Builder(this as Context)
builder.setMessage(message)
builder.setPositiveButton("", null)
val dialog = builder.create()
dialog.show()
}
}
and my manifest
<uses-permission android:name='android.permission.CAMERA'/>
<uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION"/>
<uses-permission android:name='android.permission.READ_EXTERNAL_STORAGE'/>
<uses-permission android:name='android.permission.WRITE_EXTERNAL_STORAGE'
tools:ignore= "ScopedStorage"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Hanostest1">
<activity
android:name=".addData"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>