0

我在尝试从远程 URL 创建位图时遇到了一些问题。这是片段:

    Bitmap bm = null;       

    URL aURL = null;
    try {
        aURL = new URL("http://developer.android.com/assets/images/home/honeycomb-android.png");                
    } catch (MalformedURLException e) {
        System.err.println(e);
    }

    if(aURL != null){
        URLConnection conn = null;
        BufferedInputStream bis = null;             
        InputStream is = null;

        try {
            conn = aURL.openConnection();
            conn.connect();
            is = conn.getInputStream();
            bis = new BufferedInputStream(is);
            bm = BitmapFactory.decodeStream(bis);                   
        } catch (Exception e) {
            System.err.println(e);
        } finally {
            if(bis != null){
                try {
                    bis.close();
                } catch (IOException e) {}
            }
            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {}
            }
            conn = null;
        }       
    }

我收到一条异常conn.connect()消息。它说 java.net.SocketException: Permission denied

我已经在清单中添加了权限:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          android:versionCode="1"
          android:versionName="1.0" package="mypackage.namehere">
        <uses-sdk android:minSdkVersion="8" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
        <uses-permission android:name="android.permission.INTERNET"></uses-permission>


        <application android:icon="@drawable/icon" android:label="@string/app_name">

        </application>
    </manifest>

我可以从我的计算机浏览器和模拟器浏览器加载图像。没有防火墙阻止我的互联网连接。尝试了几个 URL,但没有任何效果。

我是否正确打开连接?我应该改用 HttpConnection 吗?

4

2 回答 2

0

试试这个。

Bitmap bm = null;       

URL aURL = null;
try {
    aURL = new URL("http://developer.android.com/assets/images/home/honeycomb-android.png");                
} catch (MalformedURLException e) {
    System.err.println(e);
}

if(aURL != null){
    HttpURLConnection conn = null;
    BufferedInputStream bis = null;             
    InputStream is = null;

    try {
        conn = (HttpURLConnection) aURL.openConnection();
        is = conn.getInputStream();
        bis = new BufferedInputStream(is);
        bm = BitmapFactory.decodeStream(bis);                   
    } catch (Exception e) {
        System.err.println(e);
    } finally {
        if(bis != null){
            try {
                bis.close();
            } catch (IOException e) {}
        }
        if(is != null){
            try {
                is.close();
            } catch (IOException e) {}
        }
        conn = null;
    }       
}
于 2011-07-07T13:05:10.833 回答
0

伙计,你在错误的地方添加权限

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

关闭后你能看到吗?

于 2011-08-04T17:13:23.730 回答