无法从kotlin中的imageview下载图像,导致应用程序崩溃

yks3o0rb  于 2021-07-03  发布在  Java
关注(0)|答案(2)|浏览(417)
button.setOnClickListener {

        saveToGallery()}

这是呼叫电话的按钮 saveToGallery() 功能

private fun saveToGallery()
{

 val iv = findViewById<View>(R.id.photo_image) as ImageView
        val draw = iv.drawable

        val bitmap: Bitmap = draw.toBitmap()
        var outstream : FileOutputStream?=null
        val sdcard: File = Environment.getExternalStorageDirectory()
        val dir = File(sdcard.absolutePath + "/FlickrPics")
        dir.mkdirs()

        val filename:String = String.format("%d.png",System.currentTimeMillis())
        val outFile = File(dir,filename)
        try{
            outstream = FileOutputStream(outFile)
        } catch(e: Exception) {
            e.printStackTrace()

        }
        bitmap.compress(Bitmap.CompressFormat.PNG,100,outstream)
        try {
            if (outstream != null) {
                outstream.flush()

            }

        }
        catch (e: Exception){
            e.printStackTrace()

        }
        try {
            if (outstream != null) {
                outstream.close()
            }
        }
        catch (e: Exception){
            e.printStackTrace()
        }
snz8szmq

snz8szmq1#

private fun setupPermissions() {

        val permission =
            ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
        val permissionRead = ContextCompat.checkSelfPermission(this,Manifest.permission.READ_EXTERNAL_STORAGE)
        if (permission != PackageManager.PERMISSION_GRANTED && permissionRead != PackageManager.PERMISSION_GRANTED) {
            Log.d("TAG", "Permission to Write Storage denied")
            Log.d("TAG","Permission to Read Storage denied")
            makeRequest()
        }
    }
        private fun makeRequest() {
            ActivityCompat.requestPermissions(
                this,
                arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE)
            ,WRITE_REQUEST_CODE)
        }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        when(requestCode){
            WRITE_REQUEST_CODE,READ_REQUEST_CODE -> {
                if(grantResults.isEmpty() || grantResults[0] != PackageManager.PERMISSION_GRANTED)
                {
                    Log.i("TAG","Permission denied")
                }
                else {
                    Log.i("TAG","Permission granted")
                }
            }
        }
    }

除非您显式请求权限,否则它将不起作用,这些是请求权限的代码,只需在主活动中调用setuppermissions()

31moq8wy

31moq8wy2#

这条线上有问题

bitmap.compress(Bitmap.CompressFormat.PNG,100,outstream)

扩展为空
如果你把它放进去试试看

System.err: java.io.FileNotFoundException (No such file or directory)

结论:
您的文件不是创建的,它需要权限(在清单和运行时也需要权限)
在清单中添加这两行

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

请求权限运行时
测试代码:

private fun saveToGallery() {

    val draw = photo_image.drawable

    val bitmap: Bitmap = draw.toBitmap()
    var outstream: FileOutputStream? = null
    val sdcard: File = Environment.getExternalStorageDirectory()
    val dir = File(sdcard.absolutePath + "/FlickrPics")

    if (!dir.exists()) {
        Log.e("TAG", "is Dir Exist " + dir.exists())
        dir.mkdirs()
    } else {
        Log.e("TAG", "is Dir Exist " + dir.exists())
    }

    val filename: String = String.format("%d.png", System.currentTimeMillis())
    val outFile = File(dir, filename)
    try {
        outstream = FileOutputStream(outFile)
    } catch (e: Exception) {
        e.printStackTrace()

    }
    try {
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, outstream)
    }catch (e: Exception){
        e.printStackTrace()
    }

    try {
        outstream?.flush()

    } catch (e: Exception) {
        e.printStackTrace()

    }
    try {
        outstream?.close()
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

相关问题