Я пытаюсь добавить фотографию с камеры в свой просмотр, но получаю эту ошибку:
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { }} to activity {com.example.ohsht/com.example.ohsht.TakePhotosActivity}: java.lang.IllegalStateException: imageViewPhotoCardView must not be null
Я следовал Android Учебнику однако он показывает только пример того, как сделать это с помощью обычного ImageView в упражнении, в то время как я пытаюсь вставить фотографию в imagebutton
в обзоре переработчика. Я работал с добавлением элементов в recyclerViewAdapters раньше, но никогда с фотографиями, снятыми с камеры.
Я сталкиваюсь с ошибкой в последней части, когда пытаюсь сохранить ее в представлении переработчика.
Любая и вся помощь приветствуется.
вот мой код:
TakePhotosActivity.kt
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_take_photos)
myRecyclerView.apply {
layoutManager = LinearLayoutManager(this@TakePhotosActivity, RecyclerView.HORIZONTAL, false)
myRecyclerAdapter = RecyclerViewAdapter()
adapter = myRecyclerAdapter
}
private fun takePhotos(REQUEST_IMAGE_CAPTURE : Int) {
Intent(MediaStore.ACTION_IMAGE_CAPTURE).also { takePictureIntent ->
takePictureIntent.resolveActivity(packageManager)?.also {
val photoFile: File? = try {
createImageFile()
} catch (ex: IOException) {
// Error occurred while creating the File
// ...
null
}
// Continue only if the File was successfully created
photoFile?.also {
val photoURI: Uri = FileProvider.getUriForFile(
this,
"com.example.android.fileprovider",
it
)
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)
}
}
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
// val imageBitmap = data?.extras?.get("data") as Bitmap // <-- NO LONGER WORKS AS SHOWN IN THE TUTORIAL
val targetW: Int = imageViewPhotoCardView.width // <-- ERROR OCCURS HERE
val targetH: Int = imageViewPhotoCardView.height
val bmOptions = BitmapFactory.Options().apply {
// Get the dimensions of the bitmap
inJustDecodeBounds = true
val photoW: Int = outWidth
val photoH: Int = outHeight
// Determine how much to scale down the image
val scaleFactor: Int = Math.min(photoW / targetW, photoH / targetH)
// Decode the image file into a Bitmap sized to fill the View
inJustDecodeBounds = false
inSampleSize = scaleFactor
inPurgeable = true
}
if (requestCode == CAPTURE_CODE && resultCode == RESULT_OK) {
// myRecyclerAdapter.addPhoto(imageBitmap)
galleryAddPic()
BitmapFactory.decodeFile(currentPhotoPath, bmOptions)?.also { bitmap ->
// imageView.setImageBitmap(bitmap)
myRecyclerAdapter.addPhoto(bitmap)
}
private fun galleryAddPic() {
Intent(ACTION_MEDIA_SCANNER_SCAN_FILE).also { mediaScanIntent ->
val f = File(currentPhotoPath)
mediaScanIntent.data = Uri.fromFile(f)
sendBroadcast(mediaScanIntent)
}
}
activity_take_photos. xml
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/myRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:visibility="visible"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="@layout/photo_list_item" />
photo_list_item. xml
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="centerCrop"
android:id="@+id/imageViewPhotoCardView"
android:layout_gravity="center"
android:background="@drawable/ic_launcher_background"
android:src="@drawable/ic_launcher_foreground">
</androidx.appcompat.widget.AppCompatImageView>