这里未初始化参数“ imagePath”

时间:2019-04-29 08:57:59

标签: java android kotlin

我的代码中有这个问题,现在与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)
        }
    }
}

2 个答案:

答案 0 :(得分:0)

我认为您在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将被初始化,并且您不会收到此错误。

答案 1 :(得分:0)

尝试一下:

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