下载后 ImageView 不会更改图像

发布于 2024-12-09 14:45:32 字数 1439 浏览 0 评论 0原文

我是开发 Android 应用程序的初学者。我正在尝试从 URL 加载图像到使用线程的ImageView。我的想法是我想在不中断整个过程的情况下加载图像。

问题是 ImageView 在下载完成后不加载图像;我必须单击主页按钮才能使 ImageView 加载图像。

我的代码有错误吗?

public class imageActivity extends Activity {
    private ImageView imgView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        imgView =(ImageView)findViewById(R.id.img);

        new Thread() {
            @Override
            public void run() {
                try {
                    Drawable drawable = LoadImageFromWebOperations("http://www.androidpeople.com/wp-content/uploads/2010/03/android.png");
                    imgView.setImageDrawable(drawable);
                } catch (Exception e) {
                    Log.e("TAG","" + e);
                }
            }
        }.start();
    }

    private Drawable LoadImageFromWebOperations(String url) {
        try {
            InputStream is = (InputStream) new URL(url).getContent();
            Drawable d = Drawable.createFromStream(is, "src name");
            return d;
        } catch (Exception e) {
            System.out.println("Exc=" + e);
            return null;
        }
    }
}

I am a beginner in developing Android applications. I am trying to load an image from an URL to an ImageView using a thread. The idea is I want to load the image without interrupting overall process.

The problem is the ImageView doesn't load the image after it has finished downloading it; I must click the home button to make ImageView load the image.

Is there a mistake in my code?

public class imageActivity extends Activity {
    private ImageView imgView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        imgView =(ImageView)findViewById(R.id.img);

        new Thread() {
            @Override
            public void run() {
                try {
                    Drawable drawable = LoadImageFromWebOperations("http://www.androidpeople.com/wp-content/uploads/2010/03/android.png");
                    imgView.setImageDrawable(drawable);
                } catch (Exception e) {
                    Log.e("TAG","" + e);
                }
            }
        }.start();
    }

    private Drawable LoadImageFromWebOperations(String url) {
        try {
            InputStream is = (InputStream) new URL(url).getContent();
            Drawable d = Drawable.createFromStream(is, "src name");
            return d;
        } catch (Exception e) {
            System.out.println("Exc=" + e);
            return null;
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

两仪 2024-12-16 14:45:33

因为你应该在 UI 线程中设置你的 Drawable 。像这样

        new Thread() {
        @Override
        public void run() {
            try {
                final Drawable drawable = LoadImageFromWebOperations("http://www.androidpeople.com/wp-content/uploads/2010/03/android.png");
                imgView.post(new Runnable(){
                    imgView.setImageDrawable(drawable);
                });
            } catch (Exception e) {
                Log.e("TAG","" + e);
            }
        }
    }.start();

Because you should set your Drawable in UI thread. Like this

        new Thread() {
        @Override
        public void run() {
            try {
                final Drawable drawable = LoadImageFromWebOperations("http://www.androidpeople.com/wp-content/uploads/2010/03/android.png");
                imgView.post(new Runnable(){
                    imgView.setImageDrawable(drawable);
                });
            } catch (Exception e) {
                Log.e("TAG","" + e);
            }
        }
    }.start();
浮光之海 2024-12-16 14:45:33

我使用 Prime 来加载所有图像,它将使这样的操作变得超级简单。

I use Prime for all of my image loading, it will make actions like this super easy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文