在片段中的 ImageView 中显示拍摄的照片
Displaying taken picture in ImageView in a fragment
截至目前,我的片段中有一个 ImageView,我想在其中显示我拍摄的图像。
我现在的问题是,拍完照片后我只是黑屏,因为我的 lateinit 属性 imageView 尚未初始化。我不太确定在哪里初始化,但也许有人知道。
class Scanner : Fragment() {
private val REQUEST_CODE = 42
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,savedInstanceState: Bundle?):
View? {val view: View = inflater!!.inflate(R.layout.fragment_scan, container, false)
val btnTakePicture: Button = view.findViewById(R.id.btnTakePicture)
val imageView: ImageView = view.findViewById(R.id.imageView)
btnTakePicture.setOnClickListener {
val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivityForResult(takePictureIntent, REQUEST_CODE)
}
return view
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if(requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK){
val takenImage = data?.extras?.get("data") as Bitmap
imageView.setImageBitmap(takenImage)
}
super.onActivityResult(requestCode, resultCode, data)
}
}
Logcat 文件:
2021-05-28 15:03:18.937 8947-8947/com.example.nlp_expense_tracker E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.nlp_expense_tracker, PID: 8947
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65578, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.example.nlp_expense_tracker/com.example.nlp_expense_tracker.MainActivity}: kotlin.UninitializedPropertyAccessException: lateinit property imageView has not been initialized
at android.app.ActivityThread.deliverResults(ActivityThread.java:5015)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:5056)
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: kotlin.UninitializedPropertyAccessException: lateinit property imageView has not been initialized
at com.example.nlp_expense_tracker.fragments.Scanner.onActivityResult(Scanner.kt:44)
at androidx.fragment.app.FragmentActivity.onActivityResult(FragmentActivity.java:170)
at android.app.Activity.dispatchActivityResult(Activity.java:8310)
at android.app.ActivityThread.deliverResults(ActivityThread.java:5008)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:5056)
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
您正在绑定局部变量 imageView
,它必须是全局变量,以便您可以从所有 activity
.
访问同一个实例
class Scanner : Fragment() {
private lateinit var imageView: ImageView
private val REQUEST_CODE = 42
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,savedInstanceState: Bundle?):
View? {val view: View = inflater!!.inflate(R.layout.fragment_scan, container, false)
val btnTakePicture: Button = view.findViewById(R.id.btnTakePicture)
imageView = view.findViewById(R.id.imageView)
btnTakePicture.setOnClickListener {
val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivityForResult(takePictureIntent, REQUEST_CODE)
}
return view
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if(requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK){
val takenImage = data?.extras?.get("data") as Bitmap
imageView.setImageBitmap(takenImage)
}
super.onActivityResult(requestCode, resultCode, data)
}
}
像这样尝试一下。
截至目前,我的片段中有一个 ImageView,我想在其中显示我拍摄的图像。 我现在的问题是,拍完照片后我只是黑屏,因为我的 lateinit 属性 imageView 尚未初始化。我不太确定在哪里初始化,但也许有人知道。
class Scanner : Fragment() {
private val REQUEST_CODE = 42
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,savedInstanceState: Bundle?):
View? {val view: View = inflater!!.inflate(R.layout.fragment_scan, container, false)
val btnTakePicture: Button = view.findViewById(R.id.btnTakePicture)
val imageView: ImageView = view.findViewById(R.id.imageView)
btnTakePicture.setOnClickListener {
val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivityForResult(takePictureIntent, REQUEST_CODE)
}
return view
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if(requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK){
val takenImage = data?.extras?.get("data") as Bitmap
imageView.setImageBitmap(takenImage)
}
super.onActivityResult(requestCode, resultCode, data)
}
}
Logcat 文件:
2021-05-28 15:03:18.937 8947-8947/com.example.nlp_expense_tracker E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.nlp_expense_tracker, PID: 8947
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65578, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.example.nlp_expense_tracker/com.example.nlp_expense_tracker.MainActivity}: kotlin.UninitializedPropertyAccessException: lateinit property imageView has not been initialized
at android.app.ActivityThread.deliverResults(ActivityThread.java:5015)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:5056)
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: kotlin.UninitializedPropertyAccessException: lateinit property imageView has not been initialized
at com.example.nlp_expense_tracker.fragments.Scanner.onActivityResult(Scanner.kt:44)
at androidx.fragment.app.FragmentActivity.onActivityResult(FragmentActivity.java:170)
at android.app.Activity.dispatchActivityResult(Activity.java:8310)
at android.app.ActivityThread.deliverResults(ActivityThread.java:5008)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:5056)
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
您正在绑定局部变量 imageView
,它必须是全局变量,以便您可以从所有 activity
.
class Scanner : Fragment() {
private lateinit var imageView: ImageView
private val REQUEST_CODE = 42
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,savedInstanceState: Bundle?):
View? {val view: View = inflater!!.inflate(R.layout.fragment_scan, container, false)
val btnTakePicture: Button = view.findViewById(R.id.btnTakePicture)
imageView = view.findViewById(R.id.imageView)
btnTakePicture.setOnClickListener {
val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivityForResult(takePictureIntent, REQUEST_CODE)
}
return view
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if(requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK){
val takenImage = data?.extras?.get("data") as Bitmap
imageView.setImageBitmap(takenImage)
}
super.onActivityResult(requestCode, resultCode, data)
}
}
像这样尝试一下。