【发布时间】: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