java - 参数“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)
}
}
}
解决方案
我认为你在 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
将被初始化,你不会得到这个错误。
推荐阅读
- java - 解析字符串并将字符串文字打印到屏幕
- php - 使用 DOMDocument 更改小胡子模板中链接的 href
- python - Pygame井字游戏逻辑?我该怎么做
- mule - Mulesoft Anypoint 工作室骡子调色板文本丢失
- aws-lambda - ServerlessError:部署无服务器 AWS apigateway 时文档部分已存在
- azure-data-factory - 如何在datafactory中设置具有浮点值的变量字符串
- python-3.x - 获取日期为 YYYY-MM 减去一个月
- slack-api - Slack API:我可以获取用户的所有未读消息吗?
- mysql - 使用 docker 镜像时如何更改 MySQL 的默认配置?
- c - 如何使用给定的递归在单链表中插入节点: 1. 指向头节点的指针 2. 插入新节点的索引