Firebase 无法从外部存储RecyclerView加载ImageView

Firebase 无法从外部存储RecyclerView加载ImageView,firebase,performance,android-layout,android-fragments,fragment,Firebase,Performance,Android Layout,Android Fragments,Fragment,我正在开发一个Android应用程序,它必须将所有QR创建的图像从外部存储加载到包含RecyclerView的片段中。所有工作都进行得很顺利,但当我试图从外部存储目录的特定路径加载映像时,它未能加载并引发异常 我被这件事困住了,因为我已经给出了通往它的道路 override fun onBindViewHolder(holder: ItemHolder, position: Int) { val charItemcreate: CharItemsCreate = arrayList.g

我正在开发一个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>
}

这是我正在研究的主要片段。在这里,我有RecyclerView和CharItemClass的缩写

类FragmentViewPagerScanHistory:Fragment(){

private var charItemcreate:ArrayList?=null
私有变量customAdaptercreate:AdapterCreateHistory?=null
私有变量gridLayoutManager:gridLayoutManager?=null
//val TYPE_已保存=15
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
}
覆盖创建视图(
充气器:布局充气器,容器:视图组?,
savedInstanceState:捆绑?
):查看{
//为该碎片膨胀布局
返回充气器。充气(R.layout.fragment\u view\u pager\u scan\u history,container,false)
}
覆盖已创建的视图(视图:视图,保存状态:捆绑?){
super.onViewCreated(视图,savedInstanceState)
gridLayoutManager=gridLayoutManager(上下文,1,LinearLayoutManager.VERTICAL,false)
RecycleServiceCreateHistory?.layoutManager=gridLayoutManager
RecycleServiceCreateHistory?.setHasFixedSize(真)
charItemcreate=setImages()
customAdaptercreate=AdapterCreateHistory(this.context?:return,charItemcreate!!)
RecycleServicewFragment?.adapter=customAdaptercreate
}
private fun setImages():ArrayList{
返回charItemcreate
}
我无法将图像加载到RecyclerView,因为我遇到异常。我已附加了此调试器,它也会给我空值。。。 我也在舱单上给予了阅读许可

这是失事后的日志


您好,您可以使用Glide或毕加索库,它们将以非常合适的方式处理异常

下面是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);

我在OnBindViewHolder中添加了Glide,但这不起作用您在“Maximus”之前使用了val path:String=Environment.getExternalStorageDirectory().toString()+“Maximus/QRCreated/”请替换为该val path:String=Environment.getExternalStorageDirectory().toString()+“/Maximus/QRCreated/”add“/”这将为您提供适当的路径尝试添加路径日志,并查看路径是否正确我已添加了此项,但错误仍保留在原来的位置,根据您的代码,您必须向bitmapFactory提供jpg或png图像文件,但您只需提供文件夹的路径,以便bitmapFactory无法获取图像文件。请重试要为bitmapFactory.decodeFile()提供正确的图像路径,可以参考praveen的答案[这将为您提供可以在bitmapFactory中使用的特定图像的完整路径]
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
}
implementation 'com.github.bumptech.glide:glide:4.11.0'
Glide.with(context).load(yourpath).centerCrop().error(image that place while error occure).into(imageview);