Dynamically setting ImageView.setImageUri not displaying image

雨燕双飞 提交于 2019-12-20 03:02:36

问题


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?


回答1:


setImageURI(Uri uri) does Bitmap 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

setImageURI(Uri uri) does Bitmap reading and decoding on the UI thread, which can cause a latency hiccup. If that's a concern, consider using setImageDrawable(Drawable) or setImageBitmap(android.graphics.Bitmap) and BitmapFactory instead.

Effective solution is to use a Optimized ImageLoader library. You can use Glide as it is widely recommended.



来源:https://stackoverflow.com/questions/50220529/dynamically-setting-imageview-setimageuri-not-displaying-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!