Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
765 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...