1

我正在尝试从 URL 下载图像并将其转换为位图,但是该行

Bitmap myBitmap = BitmapFactory.decodeStream(input);

总是导致调试器跳到下一行

return null;

没有实际打印出堆栈跟踪,并且 Exception 变量也不存在于调试器中列出的变量中。我读了很多关于 url 可能存在的问题,这些问题实际上并没有导致图像、格式不正确的图像等,但它仍然存在与我肯定存在的硬编码图像相同的问题。

public static Bitmap getBitmapFromURL(String src) {
    try {
        URL url = new URL(
                "http://www.helpinghomelesscats.com/images/cat1.jpg");
        HttpURLConnection connection = (HttpURLConnection) url
                .openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

由于似乎可能涉及 manifest.xml 文件,因此我已在此处编辑添加。

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.delivery"
android:versionCode="1"
android:versionName="1.0">

<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".IntroPage"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".Browse"></activity>
    <activity android:name=".ViewProduct"></activity>
    <activity android:name=".ViewOrder"></activity>
    <activity android:name=".GetAddress"></activity>
    <activity android:name=".ConfirmOrder"></activity>
</application>

4

3 回答 3

4

以下代码适用于所有类型的图像。

    try {

        URL url = new URL("http://www.helpinghomelesscats.com/images/cat1.jpg");
        InputStream in = url.openConnection().getInputStream(); 
        BufferedInputStream bis = new BufferedInputStream(in,1024*8);
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        int len=0;
        byte[] buffer = new byte[1024];
        while((len = bis.read(buffer)) != -1){
            out.write(buffer, 0, len);
        }
        out.close();
        bis.close();

        byte[] data = out.toByteArray();
        Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
        imageView.setImageBitmap(bitmap);
    }
    catch (IOException e) {
        e.printStackTrace();
    }
于 2012-11-21T06:35:05.737 回答
0

嗨,你可以在 android 清单文件中给出这 2 个 pemssions

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

在您的活动中:

url="http://www.helpinghomelesscats.com/images/cat1.jpg"
Bitmap bmp=readBitmapFromNetwork(url);



 public static Bitmap readBitmapFromNetwork(String imgurl) {
         URL url;
         Bitmap bmp = null;
         InputStream is = null;
           BufferedInputStream bis = null;
           System.out.println("image url ==========   "+imgurl);
        try {

            url=new URL(imgurl);
            System.out.println("url.getPath()"+url.getPath());          
                try {
               URLConnection conn = url.openConnection();
                    conn.connect();
                    is = conn.getInputStream();
                    bis = new BufferedInputStream(is);
                bmp = BitmapFactory.decodeStream(bis);
                } catch (MalformedURLException e) {                 
                    System.out.println("Bad ad URL");
                    e.printStackTrace();
                } catch (IOException e) {                   
                    System.out.println("Could not get remote ad image");
                    e.printStackTrace();
                }

        } catch (MalformedURLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
         finally {
                try {
                    if( is != null )
                        is.close();
                    if( bis != null )
                      bis.close();
                } catch (IOException e) {
                    System.out.println("Error closing stream.");
                    e.printStackTrace();
                }
            }   


           return bmp;
            }
于 2012-11-21T06:45:15.683 回答
0

这对我有用,在寻找另一个问题时在这里找到了它。这样它就不会阻碍用户界面。在 ArrayAdapters 中也能很好地工作。

用途:

_coverArt = (ImageView) row.findViewById(R.id.top_CoverArt);

new DownloadImageTask(_coverArt)
            .execute("Put your URL here Hint: make sure it's valid http://www.blah...");

异步任务

class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;

public DownloadImageTask(ImageView bmImage) {
    this.bmImage = bmImage;
}

protected Bitmap doInBackground(String... urls) {
    String urldisplay = urls[0];
    Bitmap mIcon11 = null;
    try {
        InputStream in = new URL(urldisplay).openStream();
        mIcon11 = BitmapFactory.decodeStream(in);
    } catch (Exception e) {
        Log.e("Error", e.getMessage());
        e.printStackTrace();
    }
    return mIcon11;
}

protected void onPostExecute(Bitmap result) {
    bmImage.setImageBitmap(result);
}
}
于 2012-11-21T20:01:17.710 回答