简体   繁体   English

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

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

I use an API (...) I receive an url that contain a png, and I have trouble to show the picture.我使用 API (...) 我收到一个包含 png 的 url,但我无法显示图片。

I have to download the picture, and to show it.我必须下载图片,并显示它。 I put a gesture to request an other picture, so I have to delete the older picture when the user ask the newest one (when he make the gesture on the picture)我做了一个手势来请求另一张图片,所以当用户询问最新的图片时,我必须删除旧的图片(当他在图片上做手势时)

So, I've a method that download and show the picture (but the screen still black... ):所以,我有一种下载并显示图片的方法(但屏幕仍然是黑色的......):

    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();}
    }

in the onLoad, I've put在 onLoad 中,我放了

setContentView(R.layout.secondtab);

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

and I'va declared the ImageView image before @Override.我在@Override 之前声明了 ImageView 图像。

In the xml, it's like that:在 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"
/>

Thank you for the help:-)感谢您的帮助:-)

Maybe try image.postInvalidate() ?也许试试image.postInvalidate() Or just invalidate it if you are running all of this on the UI thread.如果您在 UI 线程上运行所有这些,或者只是使其无效。

My error was that there was twice the same line:我的错误是同一行有两次:

setContentView(R.layout.secondtab);

now it's running, I just have to resize it!现在它正在运行,我只需要调整它的大小!

Thanks to StackOverFlow!感谢 StackOverFlow!

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

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