0

我正在从 url 为我的图像视图调用图像,但它没有出现。我的模拟器浏览器运行完美,但任何使用互联网的应用程序都无法运行,我已在清单文件中授予用户权限。任何人请帮忙。

这是我的代码

    package com.example.simpleimage;

import java.io.InputStream;
import java.net.URL;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;

public class Simpleimage extends Activity {


    private String Url;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Url = "http://developer.android.com/images/dialog_progress_bar.png";

        ImageView iv = (ImageView)findViewById(R.id.iv);
                  Drawable drawable = LoadImageFromWebOperations(Url);
                  iv.setImageDrawable(drawable);



    }

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

}
4

2 回答 2

0

清单中的第一组

 <uses-permission android:name="android.permission.INTERNET" />

在下面的方法中传递您的 url 链接后,将返回这样的位图

imgView.setImageBitmap(getImage("Your url")) ;
public static Bitmap getImage(String url){
        Bitmap img = null ;
        try {
            URL feedImage = new URL(url);
            HttpURLConnection conn= (HttpURLConnection)feedImage.openConnection();
            InputStream is = conn.getInputStream();
            img = BitmapFactory.decodeStream(is);

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return img ;    
    }
于 2012-08-08T10:59:07.320 回答
0

看看你不在代理网络上,如果你在模拟器中设置了代理,或者使用手机测试你的应用程序。

最简单和最好的方法是执行以下操作: Android Emulator 2.2 已完成此操作

点击菜单

点击设置

点击无线和网络

前往移动网络

转到接入点名称

在这里你会 Telkila Internet,点击它。

在编辑接入点部分,输入“代理”和“端口”,还提供用户名和密码,

其余字段留空。

于 2012-08-22T12:12:28.283 回答