从相机捕获图像并存储在 Firebase 实时数据库中

时间:2021-01-03 22:14:37

标签: firebase kotlin firebase-realtime-database inventory-management

我正在 Kotlin 中为朋友的水晶标本创建一个库存应用程序。预期的逻辑是:

  • 输入输入 (app preview)
  • 添加或拍照(打开照片按钮是提交按钮下方的imgView
  • 提交
  • RockEntry 对象使用图片条目创建并添加到 Firebase 实时数据库

但是,我不确定如何将图像存储在 Firebase 实时数据库中。我已经在 J​​ava 中看到了这个过程的例子,它需要在 onActivityResult 中,在存储之前将 data.data 转换为位图,但我不完全理解来转换这个。我希望有人能解释将图像来自相机不是来自相机的图像存储到firebase中的方法和原因。

我的代码在下面,我添加了一些问题。

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

如何在构造函数中存储图片?

   }
}

主活动

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(“我需要做什么才能将图片放入 firebase?”)

                    val singularity = data.data

                    }

                }

            }

        }

    }

我想我在以下方面遇到了一些概念问题:

  • 需要做什么才能将照片从相机存储到 Firebase,
  • 从库中返回照片的方式与从实时相机返回照片的方式有何不同?

我在 Java here 中找到了一些示例(我可能应该在其中创建此项目),但此时我的转换非常难看。
这是我一直在关注的文档photobasicsDocuploadDoc

1 个答案:

答案 0 :(得分:0)

我的误解是 FireBaseDatabase 和 FireBaseStorage 是一回事。

  • 最重要的是我使用 firebase 配置了一些存储 Android Studio 中的工具(还添加了适当的依赖项)
    我声明了 private var mStorageRef: StorageReference? = null 并将其设置为等于
    mStorageRef = FirebaseStorage.getInstance().getReference("ImagesBB") 在我的 在创建。

    以下是我从我的 onActivityResult:

    val imageBitmap = data.extras?.get("data") as Bitmap
                        val baos = ByteArrayOutputStream()
                        imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos)
                        val datar = baos.toByteArray()
    
                        mStorageRef!!.putBytes(datar)
    

我仍然欢迎对帖子的输入,仍然存在的问题:

  • 我应该如何链接 FireBaseDataBase 和 FireBaseStorage。
  • 你应该如何在对象中存储图片。
  • 从库中返回照片的方式与 照片从实时相机返回的方式?


谢谢