I am creating an inventory app for a friend's crystal specimens in Kotlin. The intended logic is:
- type in input
- add or take a picture (open photo button is imgView below submit button)
- submit
- RockEntry Object is created w/ picture entry and added to Firebase RealTime DataBase
However, I am unsure how to store the image in Firebase RealTime DataBase. I have seen examples of this process in Java and it requires in the onActivityResult, data.data be converted to a bitmap before storing but I do not fully understand enough to convert this. I was hoping someone could explain the hows and whys of storing images from the camera and not from the camera into the firebase.
My code is below and I add some questions.
RockEnry.kt
package com.inven.rock_stock
import android.content.Intent
import android.graphics.Picture
import com.google.firebase.database.FirebaseDatabase
import com.google.firebase.database.core.Context
class RockEntry {
var name = ""
var purchDate = ""
var local = ""
var mine = ""
var weight = ""
var paid = ""
var asking = ""
var description = ""
var dimensions = ""
constructor(name:String,purchDate:String,local:String,mine:String,
weight:String,paid:String,asking:String,description:String,dimensions:String,){
this.name = name
this.purchDate = purchDate
this.local = local
this.mine = mine
this.weight = weight
this.paid = paid
this.asking = asking
this.description = description
this.dimensions = dimensions
how do I store picture in constructor?
MainActivity
package com.inven.rock_stock
import android.app.Activity
import android.content.ActivityNotFoundException
import android.content.Intent
import android.os.Bundle
import android.provider.MediaStore
import androidx.appcompat.app.AppCompatActivity
import com.google.firebase.database.FirebaseDatabase
import kotlinx.android.synthetic.main.activity_main.*
var CAMERA_REQUEST_CODE = 0
var database = FirebaseDatabase.getInstance().reference
class MainActivity : AppCompatActivity() {
private val TAG = "MyActivity"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button.setOnClickListener {
var name = name.text.toString()
var local = locality.text.toString()
var mine = mine.text.toString()
var weight = weight.text.toString()
var dimensions = dimensions.text.toString()
var paid = paid.text.toString()
var asking = asking.text.toString()
var description = description.text.toString()
database.child("Rocks").child(name.toLowerCase()).setValue(
RockEntry(
name,
local,
mine,
weight,
dimensions
paid,
asking,
description
)
)
}
imageBtn.setOnClickListener {
takePicture()
}
}
private fun takePicture() {
CAMERA_REQUEST_CODE = 222
val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
try {
startActivityForResult(takePictureIntent, CAMERA_REQUEST_CODE)
} catch (e: ActivityNotFoundException) {
// display error state to the user
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
CAMERA_REQUEST_CODE -> {
if (resultCode == Activity.RESULT_OK && data != null) {
println("What do I need to do to get picture into firebase?")
val singularity = data.data
}
}
}
}
}
I guess I am having some conceptual issues on:
- what needs to be done to store the photo from the camera to the firebase,
- how does the way a photo is returned from the library differ from the way a photo is returned from the live camera?
I found some examples in Java here, (which I probably should have created this project in) but my conversion is pretty ugly at this point.
here's the doc i've been staring at photobasicsDoc, uploadDoc