【问题标题】:Save image using kotlin使用 kotlin 保存图像
【发布时间】:2023-01-03 12:08:27
【问题描述】:

我是 Android 开发的新手,我正在制作捕获图像然后保存的简单应用程序。我可以捕获图像并显示到图像视图中,但不幸的是我无法保存到图库中。

我尝试了很多方法,但无法保存。这是获取和显示的原始代码。

// EDITED //

我添加了保存文件的功能,但仍然无法正常工作

class TomarFotos : AppCompatActivity() {

    lateinit var photoPath: String
    val REQUEST_IMAGE_CAPTURE = 1
    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_tomar_fotos)
        val foto = findViewById<Button>(R.id.tomarfotos)
        foto.setOnClickListener{
            tomarFoto()
        }
    }
    fun tomarFoto()  {
        val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        if(intent.resolveActivity(packageManager) != null){

            var photoFile: File? = null
            try{
                photoFile = createImageFile()
            }catch (e: IOException){}
            if(photoFile != null){
                val photoUri = FileProvider.getUriForFile(
                    this,
                    "com.example.android.fileprovider",
                    photoFile
                )
                intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri)
                startActivityForResult(intent,REQUEST_IMAGE_CAPTURE)
            }
       }
    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        val fotoTomada = findViewById<ImageView>(R.id.fotopreview)
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            fotoTomada.rotation = 90f
            fotoTomada.setImageURI(Uri.parse(photoPath))
        }
    }
    private fun createImageFile(): File? {
            val fileName = "foto"
            val storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES)
            val image = File.createTempFile(
                fileName,
                ".jpg",
                storageDir
            )
            photoPath = image.absolutePath
            return image

    }
}


i try developer.android.com already but didnt work

【问题讨论】:

  • 你没有捕捉图像。您得到的只是可能图像的缩略图。更改您的代码,以便相机应用程序将图像文件写入您确定的位置。
  • 已经改了,还是不行

标签: android kotlin


【解决方案1】:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    val fotoTomada = findViewById<ImageView>(R.id.fotopreview)
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        val imageBitmap = data?.extras?.get("data") as Bitmap
        fotoTomada.setImageBitmap(imageBitmap)
        saveImage(imageBitmap)
    }
}

调用此函数将图像保存到图库

private fun saveImage(bitmap: Bitmap) {
    var outStream: FileOutputStream? = null
    // Write to SD Card
    try {
        val dir = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + File.separator + "AppName/")
        dir.mkdirs()
        val fileName = "IMG_${System.currentTimeMillis()}"
        val outFile = File(dir, fileName)
        outStream = FileOutputStream(outFile)
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream)
        outStream.flush()
        outStream.close()
        Toast.makeText(this@MainActivity, "Image Saved Successfully", Toast.LENGTH_LONG).show()
    } catch (e: FileNotFoundException) {
        Log.d("TAG", "saveImage: ${e.message}")
    } catch (e: IOException) {
        Log.d("TAG", "saveImage: ${e.message}")
    } 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-08
    • 2013-03-19
    • 1970-01-01
    • 1970-01-01
    • 2017-03-17
    • 2020-10-08
    • 2020-04-16
    相关资源
    最近更新 更多