【问题标题】:Unable To Load ImageView from External Storage RecyclerView无法从外部存储 RecyclerView 加载 ImageView
【发布时间】:2020-10-10 04:39:10
【问题描述】:

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

var imageload: Bitmap? = null

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

}

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

类 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

【问题讨论】:

    标签: firebase performance android-layout android-fragments fragment


    【解决方案1】:

    您好,您可以使用 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);
    

    【讨论】:

    • 我在 OnBindViewHolder 中添加了 Glide 但这没有用
    • 你使用了 val 路径:String = Environment.getExternalStorageDirectory().toString() + "Maximus/QRCreated/" 请用这个 val 路径替换:String = Environment.getExternalStorageDirectory().toString() + "/Maximus/QRCreated/" 在 "Maximus" 之前添加 "/" 这将为您提供适当的路径尝试添加路径日志并查看路径是否正确
    • 我已经添加了这个但是错误仍然在它已经是的相同位置
    • 根据您的代码,您必须向 bitmapFactory 提供 jpg 或 png 图像文件,但您只提供文件夹的路径,因此 bitmapFactory 无法获取图像文件。尝试为 bitmapFactory.decodeFile() 提供正确的图像路径;
    • 您可以参考 praveen s 的回答 [stackoverflow.com/questions/14858694/…,这将为您提供可以在 bitmapfactory 中使用的特定图像的完整路径
    猜你喜欢
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 2020-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多