无法从外部存储 RecyclerView 加载 ImageView

Unable To Load ImageView from External Storage RecyclerView

我正在开发一个 Android 应用程序,它必须将所有 QR 创建的图像从外部存储加载到包含 RecyclerView 的片段。所有的工作都很顺利,但是当我试图从外部存储目录的特定路径加载图像时,它加载失败并抛出异常..

我坚持这个,因为我已经给出了它的路径

override fun onBindViewHolder(holder: ItemHolder, position: Int) {

    val charItemcreate: CharItemsCreate = arrayList.get(position)

    val path: String = Environment.getExternalStorageDirectory().toString() + "Maximus/QRCreated/"
    val bitmap = BitmapFactory.decodeFile(path)

     //holder.images = charItemcreate.imageload.toIcon()
}

这是我为 RecyclerView 创建的布局..

  <androidx.constraintlayout.widget.ConstraintLayout

    android:id="@+id/constmain"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    tools:ignore="MissingConstraints">

    <ImageView
        android:layout_width="@dimen/_62sdp"
        android:layout_height="@dimen/_62sdp"
        android:id="@+id/imgrecyclerviewcreate"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />
    <TextView
        android:id="@+id/txt_nametext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toEndOf="@id/imgrecyclerviewcreate"
        android:text="@string/email"/>

    <TextView
        android:id="@+id/txt_detail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toEndOf="@id/imgrecyclerviewcreate"
        app:layout_constraintTop_toBottomOf="@+id/txt_nametext"
        android:text="@string/email"/>
</androidx.constraintlayout.widget.ConstraintLayout>

适配器 Class 我已经在给定图像文件夹路径的地方创建了

class AdapterCreateHistory(var context: Context, var arrayList: ArrayList<CharItemsCreate>) :
RecyclerView.Adapter<AdapterCreateHistory.ItemHolder>() {


override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemHolder {
    val viewHolder = LayoutInflater.from(parent.context)
        .inflate(R.layout.createhistoryitem, parent, false)
    return ItemHolder(viewHolder)
}

override fun getItemCount(): Int {
    return arrayList.size
}

override fun onBindViewHolder(holder: ItemHolder, position: Int) {

    val charItemcreate: CharItemsCreate = arrayList.get(position)

    val path: String = Environment.getExternalStorageDirectory().toString() + "Maximus/QRCreated/"
    val bitmap = BitmapFactory.decodeFile(path)

     //holder.images = charItemcreate.imageload.toIcon()
}
class ItemHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    var images = itemView.findViewById<ImageView>(R.id.imgrecyclerviewcreate)

}

这是 Kotlin 模型 Class

class CharItemsCreate {

var imageload: Bitmap? = null

constructor(imageload: Bitmap) {
    this.imageload = imageload
}

}

这是我正在处理的主要片段。在这里我有 Initialez RecyclerView 和 CharItemClass

class FragmentViewPagerScanHistory : Fragment() {

private var charItemcreate: ArrayList<CharItemsCreate>? = null
private var customAdaptercreate: AdapterCreateHistory? = null
private var gridLayoutManager: GridLayoutManager? = null

//val TYPE_SAVED = 15

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

}

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_view_pager_scan_history, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    gridLayoutManager = GridLayoutManager(context, 1, LinearLayoutManager.VERTICAL, false)
    recyclerviewcreatehistory?.layoutManager = gridLayoutManager
    recyclerviewcreatehistory?.setHasFixedSize(true)
    charItemcreate = setImages()
    customAdaptercreate = AdapterCreateHistory(this.context ?: return, charItemcreate!!)
    recyclerViewfragment?.adapter = customAdaptercreate
}

private fun setImages(): ArrayList<CharItemsCreate>? {
    return charItemcreate
}

我遇到异常,无法将图像加载到 RecyclerView 中。我已经附加了调试器它也给我空值... 我也在清单中给予了读取权限

这是Logcat崩溃后的

您好,您可以使用 Glide 或 picasso 库,它们会以非常合适的方式处理异常,

这里是 Glide 的实现:-

implementation 'com.github.bumptech.glide:glide:4.11.0'

这是代码块:-

Glide.with(context).load(yourpath).centerCrop().error(image that place while error occure).into(imageview);