来自URL的Android位图始终为null

时间:2012-11-21 05:13:07

标签: java android image bitmap bitmapfactory

我正在尝试从网址下载图片并将其转换为位图,但行

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>

3 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:0)

嗨,你可以在android清单文件中给这两个人

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

答案 2 :(得分:0)

这对我有用,在搜索另一个问题时就在这里找到它。这样它就不会占用UI。在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...");

Asynctask

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