java - 参数 'imagePath'此处未初始化

标签 java android kotlin

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

此处未初始化“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)
        }
    }
}

最佳答案

试试这个:

fun shareIt(imagePath: File? = this.imagePath) {

关于java - 参数 'imagePath'此处未初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55899972/

相关文章:

java - String getChars() 方法混淆

java - 使用 Hibernate 调用实体方法

android - 在 react native 应用程序中启用/禁用 Firebase 推送通知

Android 实时数据库抛出错误 : client is offline but it's not

java - 客户/服务器多人生存游戏应该使用什么? java

java - 许多线程处于 IN_NATIVE 状态

java - 没有 Java 的 Android

android - java.net.SocketException : sendto failed: EPIPE (Broken pipe) over a data connection to localhost 异常

Android:为什么 SQLite 表中的文本字段需要 getBlob() 函数?

kotlin - Ktor-静态内容路由