首页 > 解决方案 > 参数“imagePath”在这里未初始化

问题描述

我在我的代码中遇到了这个问题,现在使用 kotlin 在 android 上运行良好我遇到了这个错误,有人可以帮忙吗?

此处未初始化“imagePath”参数。但我不知道将 imagePath 放在哪里才能正确启动,有没有人指导我可以将它放入代码中以正常工作?

C:\COMPARTILHAR\app\src\main\java\calculadora\franquia\compartilhar\MainActivity.kt: (44, 40): 参数 'imagePath' 未初始化她

编辑:清洁代码

class MainActivity : AppCompatActivity() {

    private var scrollView: ScrollView? = null
    private var imagePath: File? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        //create bitmap from the ScrollView
        fun getBitmapFromView(view: View, height: Int, width: Int): Bitmap {
            val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
            val canvas = Canvas(bitmap)
            val bgDrawable = view.background
            if (bgDrawable != null)
                bgDrawable.draw(canvas)
            else
                canvas.drawColor(Color.WHITE)
            view.draw(canvas)
            return bitmap
        }

        ERROR LINE fun shareIt(imagePath: File? = imagePath) {

            val uri = FileProvider.getUriForFile(this@MainActivity, BuildConfig.APPLICATION_ID + ".provider", imagePath!!)

            val sharingIntent = Intent(Intent.ACTION_SEND)
            sharingIntent.type = "image/*"
            val shareBody = "APP"
            sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "APP")
            sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody)
            sharingIntent.putExtra(Intent.EXTRA_STREAM, uri)

            startActivity(Intent.createChooser(sharingIntent, "SARE VIA"))
        }
        var share = findViewById<View>(R.id.share) as Button
        share = findViewById<View>(R.id.share) as Button
        share.setOnClickListener {
            val bitmap = getBitmapFromView(scrollView!!, scrollView!!.getChildAt(0).height, scrollView!!.getChildAt(0).width)
            saveBitmap(bitmap)
            shareIt()
        }
    }
    @Throws(IOException::class)
    private fun createScreenShotImageFile(): File {
        var mediaStorageDir = File(
            Environment.getExternalStorageDirectory(),
            "YourAppName"
        )
        var screenShotDirectory = "${mediaStorageDir}/screenShots"
        val file = File(screenShotDirectory)
        if (!file.exists()) {
            file.mkdirs()
        }
        val timeStamp = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
        val imageFileName = "screeShotImage-$timeStamp.png"
        return File(screenShotDirectory, imageFileName)
    }

    fun saveBitmap(bitmap: Bitmap) {
        imagePath =  createScreenShotImageFile()
        val fos: FileOutputStream
        try {
            fos = FileOutputStream(imagePath)
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos)
            fos.flush()
            fos.close()
        } catch (e: FileNotFoundException) {
            Log.e("GREC", e.message, e)
        } catch (e: IOException) {
            Log.e("GREC", e.message, e)
        }
    }
}

标签: javaandroidkotlin

解决方案


我认为你在 saveBitmap() 方法中做错了,试试这种方式

  @Throws(IOException::class)
private fun createScreenShotImageFile(): File {
    var mediaStorageDir = File(
        Environment.getExternalStorageDirectory(),
        "YourAppName"
    )
    var screenShotDirectory = "${mediaStorageDir}/screenShots"
    val file = File(screenShotDirectory)
    if (!file.exists()) {
        file.mkdirs()
    }
    val timeStamp = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
    val imageFileName = "screeShotImage-$timeStamp.png"
    return File(screenShotDirectory, imageFileName)
}

fun saveBitmap(bitmap: Bitmap) {
    imagePath =  createScreenShotImageFile()
    val fos: FileOutputStream
    try {
        fos = FileOutputStream(imagePath)
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos)
        fos.flush()
        fos.close()
    } catch (e: FileNotFoundException) {
        Log.e("GREC", e.message, e)
    } catch (e: IOException) {
        Log.e("GREC", e.message, e)
    }
}

通过这样做imagePath将被初始化,你不会得到这个错误。


推荐阅读