我需要在应用程序中具有拍照功能,然后将其存储在 recyclerView 中
- 问题是我的 AlertDialog 中没有显示带有拍摄图像的回收站视图。
- 我怀疑两种可能的结果是我是否在我的活动中错误地设置了回收站视图,或者我的适配器中没有正确提供图像。
如下提供:
负责显示对话框的函数
private fun showRequestAcceptDialog() {
var listOfPhotoFileNames: MutableList<File?> = arrayListOf()
var listOfPhotoFileURIs: MutableList<Uri> = arrayListOf()
val dialogBuilder = AlertDialog.Builder(this)
dialogBuilder.setMessage("Принятие ЧО")
val dialogView = layoutInflater.inflate(R.layout.dialog_accept_draft, null)
val btnMakePhoto = dialogView.findViewById<Button>(R.id.btnMakePhoto)
val etComment = dialogView.findViewById<EditText>(R.id.etDialogAcceptDraft)
val recyclerView = dialogView.findViewById<RecyclerView>(R.id.rvAcceptPhotos)
ivDialogAcceptPhoto = dialogView.findViewById<ImageView>(R.id.ivDialogAcceptDraft)
ivDialogAcceptPhoto_1 = dialogView.findViewById<ImageView>(R.id.ivDialogAcceptDraft_1)
etComment.hint = "Комментарий"
dialogBuilder.setCancelable(false)
dialogBuilder.setView(dialogView)
adapter_dialog = RequestCheckStatusListDialogAdapter()
recyclerView.adapter = adapter_dialog
val layoutManager = LinearLayoutManager(this@RequestCheckStatusListActivity)
recyclerView.layoutManager = layoutManager
btnMakePhoto?.setOnClickListener {
Intent(MediaStore.ACTION_IMAGE_CAPTURE).also { takePictureIntent ->
// Ensure that there's a camera activity to handle the intent
takePictureIntent.resolveActivity(packageManager)?.also {
// Create the File where the photo should go
val photoFile: File? = try {
createImageFile()
} catch (ex: IOException) {
ex.printStackTrace()
null
}
listOfPhotoFileNames.add(photoFile)
listOfPhotoPaths.add(currentPhotoPath)
// Continue only if the File was successfully created
photoFile?.also {
val photoURI: Uri = FileProvider.getUriForFile(
this,
applicationContext.packageName + ".fileprovider",
it
)
listOfPhotoFileURIs.add(photoURI)
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)
}
}
}
}
dialogBuilder.setPositiveButton("Подтвердить", null)
dialogBuilder.setNegativeButton("Отмена", null)
acceptRequestDialog = dialogBuilder.show()
acceptRequestDialog!!.getButton(android.app.AlertDialog.BUTTON_POSITIVE)
.setOnClickListener {
if (currentPhotoPath != null) {
presenter.acceptRequest(etComment.text.toString(), currentPhotoPath)
showToast("$listOfPhotoPaths")
}
else
{
showToast("Прикрепите договор")
}
}
acceptRequestDialog!!.getButton(android.app.AlertDialog.BUTTON_NEGATIVE)
.setOnClickListener {
closeAccpeptDialog()
}
}
recyclerView 的项目
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_marginTop="16dp"
android:id="@+id/ivItemPhoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_baseline_camera_alt_24" /></androidx.constraintlayout.widget.ConstraintLayout>
对话框布局
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/etDialogAcceptDraft"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnMakePhoto"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Сделать фото"
android:layout_marginTop="24dp"
app:layout_constraintBottom_toTopOf="@+id/ivDialogAcceptDraft"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/etDialogAcceptDraft"></Button>
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/ivDialogAcceptDraft"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="24dp"
android:src="@drawable/ic_baseline_camera_alt_24"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.273"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnMakePhoto" />
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/ivDialogAcceptDraft_1"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/ic_baseline_camera_alt_24"
app:layout_constraintBottom_toTopOf="@+id/rvAcceptPhotos"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.317"
app:layout_constraintStart_toEndOf="@+id/ivDialogAcceptDraft"
app:layout_constraintTop_toBottomOf="@+id/btnMakePhoto"
app:layout_constraintVertical_bias="1.0" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvAcceptPhotos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ivDialogAcceptDraft"
>
</androidx.recyclerview.widget.RecyclerView>
</androidx.constraintlayout.widget.ConstraintLayout>
适配器
class RequestCheckStatusListDialogAdapter : RecyclerView.Adapter<RequestCheckStatusListDialogAdapter.Holder>() {
private var _data: MutableList<requestCheckType> = mutableListOf()
private var mCallback: Callback? = null
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
val itemView =
LayoutInflater.from(parent.context)
.inflate(R.layout.item_request_draft_history, parent, false)
return Holder(itemView)
}
fun clearData() {
_data.clear()
notifyDataSetChanged()
}
override fun getItemCount() = _data.size
@SuppressLint("SetTextI18n")
override fun onBindViewHolder(holder: Holder, position: Int): Unit = with(_data[position]) {
holder.apply {
val file = File(AppConstant.FULL_MEDIA_PHOTO_PATH + draft_contract_file_name)
val file_url = draft_contract_file_url
if (file.exists()) {
ivPhoto.visibility = View.VISIBLE
GlideApp.with(itemView.context)
.load(file)
.override(128, 128)
.skipMemoryCache(true)
.centerCrop()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(ivPhoto)
} else {
//ivPhoto.visibility = View.INVISIBLE
GlideApp.with(itemView.context)
.load(AppConstant.getServerName() + file_url)
.override(128, 128)
.skipMemoryCache(true)
.centerCrop()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(ivPhoto)
}
itemView.setOnClickListener {
mCallback!!.onClick(this@with)
}
/*
ivPhoto.setOnClickListener {
var intent = Intent(itemView.context.applicationContext, PhotoShowUpActivity::class.java)
intent.putExtra("file_path_key", file)
itemView.context.applicationContext.startActivity(intent)
}
*/
}
}
fun setCallback(callback: Callback) {
this.mCallback = callback
}
interface Callback {
fun onClick(requestListEntity: requestCheckType)
}
class Holder(view: View) : RecyclerView.ViewHolder(view) {
val ivPhoto = view.ivItemPhoto
}
}