0

I am working on an Android app which has to load all QR created image from external storage to the fragment which contains a RecyclerView. All the work goes fine, but as I tried to load the image from the specific path from external storage directory, it fails to load and throws an exception..

I am stuck with this as I have given the path to it like

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()
}

Here is the Layout I Created For 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>

The Adapter Class I have Created where i have Given the Images Folder path

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)

}

Here is the Kotlin Model Class

class CharItemsCreate {

var imageload: Bitmap? = null

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

}

Here is the Main Fragment I am Working on. In Here I have Initialez the RecyclerView and the 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
}

I am Unable to Load Images into RecyclerView as I Am Getting Exception . I have Attached the Debugger With this It Also Give me the Null Values... I have Also Given the Read Permission in the Manifest

Here is the Logcat After It Crashed

Image of Locat

Usman Ali
  • 425
  • 1
  • 9
  • 31

1 Answers1

1

Hello you can use Glide or picasso library, that will handle exception in very appropriate way,

here is the implementation of Glide :-

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

This is is the code block :-

Glide.with(context).load(yourpath).centerCrop().error(image that place while error occure).into(imageview);
Kashyap Rathod
  • 275
  • 1
  • 9
  • I have added the Glide in the OnBindViewHolder but this didnt Worked – Usman Ali Oct 10 '20 at 05:39
  • 1
    you have used val path: String = Environment.getExternalStorageDirectory().toString() + "Maximus/QRCreated/" please replace with this val path: String = Environment.getExternalStorageDirectory().toString() + "/Maximus/QRCreated/" add "/" before "Maximus" this will give you appropriate path try to add log of path and see if the path if correct or not – Kashyap Rathod Oct 10 '20 at 06:27
  • 1
    I have Added this But the Error Remains on the Same Position Where it Already is – Usman Ali Oct 10 '20 at 06:34
  • According to your code you have to provide jpg or png image file to bitmapFactory but you have only provide the folder's path so bitmapFactory can not get image file. try to give proper image path to bitmapFactory.decodeFile(); – Kashyap Rathod Oct 10 '20 at 06:39
  • What I need In Regarding to Loading as I only Want to Load All the Images in that Folder – Usman Ali Oct 10 '20 at 06:45
  • 1
    you can refer praveen s's answer [https://stackoverflow.com/questions/14858694/retrieve-images-of-particular-folder-in-android] that will give you full path of specific image that you can use in bitmapfactory – Kashyap Rathod Oct 10 '20 at 07:00