Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
797 views
in Technique[技术] by (71.8m points)

java - Dynamically setting ImageView.setImageUri not displaying image

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...