![](/img/trans.png)
[英]Android Kotlin copy App file to “Files” App through Intent
[英]How to get the bmp extension files through the intent - Android kotlin
我想通过意图选择扩展名为“bmp”的图像。 我试过传递“图像/bmp”,但没有成功。 那么通过意图获取“bmp”扩展图像的工作方式是什么。
将其转换为字符串。 首先,您需要将文件转换为 .jpg 或 .png 文件(您必须在代码之外手动执行此操作),然后转换为 bitmap:
Bitmap bmp = BitmapFactory.decodeFile( PathToFileString );
或者
Bitmap bmp = BitmapFactory.decodeFileDescriptor( fileDescriptorObject );
然后
fun encode(imageUri: Uri): String {
val input = activity.getContentResolver().openInputStream(imageUri)
val image = BitmapFactory.decodeStream(input , null, null)
// Encode image to base64 string
val baos = ByteArrayOutputStream()
image.compress(Bitmap.CompressFormat.JPEG, 100, baos)
var imageBytes = baos.toByteArray()
val imageString = Base64.encodeToString(imageBytes, Base64.DEFAULT)
return imageString
}
fun decode(imageString: String) {
// Decode base64 string to image
val imageBytes = Base64.decode(imageString, Base64.DEFAULT)
val decodedImage = BitmapFactory.decodeByteArray(imageBytes, 0,
imageBytes.size)
imageview.setImageBitmap(decodedImage)
}
获得字符串后,将其放入意图中:
val intent = Intent(this, YourClass::class.java)
intent.putExtra("image", yourString);
startActivity(intent)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.