读取sqlite数据库时出错:数据库'livesqlitedatabaseid>?

ojsjcaue  于 2021-09-29  发布在  Java
关注(0)|答案(0)|浏览(511)

很抱歉包含了这么长的代码。我试图从youtube上的教程中学习,但视频教程缺少解释。教程视频使用java,但我尝试为我的kotlin应用程序实现它。但当我尝试拍照时,我会出现以下错误:

Error reading Sqlite database: Database 'LiveSqliteDatabaseId(path=/data/data/com.example.b1/databases/google_app_measurement_local.db,name=google_app_measurement_local.db, connectionId=1) not found

为什么我会犯这个错误?我的代码有什么问题?
我还制作了textview,可以点击它来选择类别,但它不起作用。代码中的categorydialog()函数似乎有问题。这就是导致上述错误的原因吗?
我的代码:

class AddProductActivity : AppCompatActivity() {
    private val CAMERA_REQUEST_CODE:Int =200
    private val STORAGE_REQUEST_CODE:Int =300
    private val IMAGE_PICK_GALLERY_CODE:Int =400
    private val IMAGE_PICK_CAMERA_CODE:Int =500

    lateinit var cameraPermission: Array<String>
    lateinit var storagePermission: Array<String>
    lateinit var image_Uri:Uri

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_add_product)
        cameraPermission= arrayOf(android.Manifest.permission.CAMERA, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
        storagePermission= arrayOf(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)

        item_image.setOnClickListener{
            showImagePickDialog()
        }
        tv_kategori.setOnClickListener{
            categoryDialog()
        }
        tambahkan_barang.setOnClickListener{
            inputData()
        }
    }
    private lateinit var kategori:  String
    private lateinit var berat_sampah:  String
    private lateinit var deskripsi:  String

    private fun inputData(){
        kategori=tv_kategori.text.toString().trim({it<=' '})
        berat_sampah=et_berat_sampah.text.toString().trim({it<=' '})
        deskripsi=et_deskripsi.toString().trim({it<=' '})

        if(TextUtils.isEmpty(kategori)){
            Toast.makeText(
                this@AddProductActivity,
                "Harap memilih kategori",
                Toast.LENGTH_SHORT)
                .show()
            return
        }
        if(TextUtils.isEmpty(berat_sampah)){
            Toast.makeText(
                this@AddProductActivity,
                "Harap mengisi berat sampah",
                Toast.LENGTH_SHORT)
                .show()
            return
        }
        if(TextUtils.isEmpty(deskripsi)){
            Toast.makeText(
                this@AddProductActivity,
                "Harap mengisi deskripsi singkat",
                Toast.LENGTH_SHORT)
                .show()
            return
        }
        addProduct()
    }

    private  fun addProduct(){

        val timeStamp:String=""+System.currentTimeMillis()

        if(image_Uri==null){
            val hashMap:HashMap<String,String> = HashMap<String,String>()
            hashMap.put("productId",timeStamp)
            hashMap.put("productCategory",kategori)
            hashMap.put("productWeight",berat_sampah)
            hashMap.put("productDescription",deskripsi)
            hashMap.put("productIcon","")
            //hashMap.put("uid",""+firebaseAuth.uid)

        }

    }
    private fun categoryDialog(){
        val builder= AlertDialog.Builder(this)
        builder.setTitle("Kategori Sampah").setItems(Constants.KATEGORI_BARANG){dialog,which ->{
            val category= Constants.KATEGORI_BARANG[which]
            tv_kategori.setText(category)
        }}.show()
    }
    private fun showImagePickDialog(){
        val options: Array<String> = arrayOf("Kamera","Gallery")
        val builder= AlertDialog.Builder(this)
        builder.setTitle("Pilih Gambar").setItems(options) { dialog, which ->
            // put your logic in here
            if(which==0){
                if(checkCameraPermission()){
                    pickFromCamera()
                }
                else{
                    requestCameraPermission()
                }
            }
            else{
                if(checkStoragePermission()){
                    pickFromGallery()
                }
                else{
                    requestStoragePermission()
                }
            }
        }.show()
    }
    private fun pickFromGallery(){
        val intent= Intent(Intent.ACTION_PICK)
        intent.setType("image/*")
        startActivityForResult(intent,IMAGE_PICK_GALLERY_CODE)
    }
    private fun pickFromCamera(){
        val contentValues= ContentValues()
        contentValues.put(MediaStore.Images.Media.TITLE,"Temp_Image_Title")
        contentValues.put(MediaStore.Images.Media.DESCRIPTION,"Temp_Image_Description")

        image_Uri= contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)!!
        intent= Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        intent.putExtra(MediaStore.EXTRA_OUTPUT,image_Uri)
        startActivityForResult(intent, IMAGE_PICK_CAMERA_CODE)

    }
    private fun checkStoragePermission(): Boolean{
        val result :Boolean=ContextCompat.checkSelfPermission(this@AddProductActivity, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)==(PackageManager.PERMISSION_GRANTED)
        return result
    }
    private fun requestStoragePermission(){
        ActivityCompat.requestPermissions(this,storagePermission,STORAGE_REQUEST_CODE)
    }
    private fun checkCameraPermission(): Boolean{
        val result :Boolean=ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA)==(PackageManager.PERMISSION_GRANTED)
        val result1 :Boolean=ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)==(PackageManager.PERMISSION_GRANTED)
        return  result && result1
    }
    private fun requestCameraPermission(){
        ActivityCompat.requestPermissions(this,cameraPermission,STORAGE_REQUEST_CODE)
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        when (requestCode){
            CAMERA_REQUEST_CODE->{
                if(grantResults.size>0){
                    val cameraAccepted:Boolean=grantResults[0]==PackageManager.PERMISSION_GRANTED
                    val storageAccepted:Boolean=grantResults[1]==PackageManager.PERMISSION_GRANTED
                    if(cameraAccepted && storageAccepted){
                        pickFromCamera()
                    }
                    else{
                        Toast.makeText(
                            this@AddProductActivity,
                            "Akses Kamera dan Penyimpanan dibutuhkan",
                            Toast.LENGTH_SHORT)
                            .show()
                    }
                }
            }
            STORAGE_REQUEST_CODE->{
                if(grantResults.size>0){
                    val storageAccepted:Boolean=grantResults[0]==PackageManager.PERMISSION_GRANTED
                    if(storageAccepted){
                        pickFromGallery()
                    }
                    else{
                        Toast.makeText(
                            this@AddProductActivity,
                            "Akses Penyimpanan dibutuhkan",
                            Toast.LENGTH_SHORT)
                            .show()
                    }
                }
            }
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if(resultCode== RESULT_OK){
            if(requestCode == IMAGE_PICK_GALLERY_CODE){
                //img pick from gallery

                //save picked img
                image_Uri= data!!.getData()!!
                //set IMG
                item_image.setImageURI(image_Uri)
            }
            else if(requestCode==IMAGE_PICK_CAMERA_CODE){
                item_image.setImageURI(image_Uri)
            }
        }
        super.onActivityResult(requestCode, resultCode, data)
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题