繁体   English   中英

如何下载图像并将其放入 Android 的视图中

[英]How can I download and put an image in a view in Android

我使用 API (...) 我收到一个包含 png 的 url,但我无法显示图片。

我必须下载图片,并显示它。 我做了一个手势来请求另一张图片,所以当用户询问最新的图片时,我必须删除旧的图片(当他在图片上做手势时)

所以,我有一种下载并显示图片的方法(但屏幕仍然是黑色的......):

    private void downloadImage() {
    Bitmap bitmap = null;
    try {
        URL urlImage = new URL("http://www.google.fr/intl/en_com/images/srpr/logo1w.png");
        HttpURLConnection connection = (HttpURLConnection) urlImage.openConnection();
        InputStream inputStream = connection.getInputStream();
        bitmap = BitmapFactory.decodeStream(inputStream);
        Log.v("", "is showed");
        image.setImageBitmap(bitmap);
        Log.v("", "isn't");
        } 
    catch (MalformedURLException e) {
        e.printStackTrace();
        } 
    catch (IOException e) {
        e.printStackTrace();}
    }

在 onLoad 中,我放了

setContentView(R.layout.secondtab);

图像 = (ImageView)findViewById(R.id.ivImage);

我在@Override 之前声明了 ImageView 图像。

在 xml 中是这样的:

<?xml version="1.0" encoding="utf-8"?>

<android.gesture.GestureOverlayView
    android:id="@+id/gestures" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:layout_weight="1.0"
    android:eventsInterceptionEnabled="true" android:gestureStrokeType="single" 
    android:gestureColor="#00FFFFFF"/>

<ImageView 
android:id="@+id/ivImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical"
/>

感谢您的帮助:-)

也许试试image.postInvalidate() 如果您在 UI 线程上运行所有这些,或者只是使其无效。

我的错误是同一行有两次:

setContentView(R.layout.secondtab);

现在它正在运行,我只需要调整它的大小!

感谢 StackOverFlow!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM