无法从 Kotlin 中的 ImageView 下载图像导致应用程序崩溃
Unable to download Image from ImageView in Kotlin Causing App Crash
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()
}
这条线路有问题
bitmap.compress(Bitmap.CompressFormat.PNG,100,outstream)
外流为空
如果你把它放在 try catch 中,你就会看到
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()
}
}
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")
}
}
}
}
除非您明确请求权限,否则它不会起作用,这些是请求权限的代码,只需在 main activity
中调用 setupPermissions()
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()
}
这条线路有问题
bitmap.compress(Bitmap.CompressFormat.PNG,100,outstream)
外流为空
如果你把它放在 try catch 中,你就会看到
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()
}
}
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")
}
}
}
}
除非您明确请求权限,否则它不会起作用,这些是请求权限的代码,只需在 main activity
中调用 setupPermissions()