I have the following scenario:
I am dynamically assigning an image URI to an ImageView, and then dynamically adding this ImageView to a HorizontalView.
The problem is, I don't see these images loaded in the ImageView, inside the HorizontalView.
Here is my code:
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="100dp">
<LinearLayout
android:id="@+id/imageDisplayer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView>
Here is my code for loading the images:
public void LoadImages(ArrayList<PhotoPath> photos){
LinearLayout layout = (LinearLayout)findViewById(R.id.imageDisplayer);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
if (photos != null) {
for (PhotoPath photo : photos) {
Uri photoUri = Uri.parse(photo.getPhotoPath());
params.setMargins(10, 10, 10, 10);
params.gravity = Gravity.NO_GRAVITY;
ImageView imageView = new ImageView(this);
imageView.setImageURI(photoUri);
imageView.setLayoutParams(params);
layout.addView(imageView);
}
}
}
Here is an example of my URI:
content://com.android.providers.media.documents/document/image%3A48
What am I doing wrong?
setImageURI(Uri uri)
doesBitmap
reading and decoding on the UI thread . So this may cause delay in MainThread cause image size can be varry and it will take time to load . And as its on Main Thread so it can cause letency issue.From the Documentation
Effective solution is to use a Optimized ImageLoader library. You can use Glide as it is widely recommended.