2012-03-01 60 views
0

当我调用这个方法时,我总是得到NULL的响应,但假如我在浏览器中运行它,它会显示图像。 URL myFileUrl = null;无法在android下载图像

try { 
     myFileUrl = new URL("http://www.russiawear.com/components/com_virtuemart/shop_image/product/youth_russia_usa_4e2f7f78b543c.jpg"); 

     HttpURLConnection conn = (HttpURLConnection) myFileUrl 
       .openConnection(); 
     conn.setDoInput(true); 
     conn.connect(); 
     InputStream is = conn.getInputStream(); 

     BitmapFactory.Options options = new BitmapFactory.Options(); 

     bmImg = BitmapFactory.decodeStream(is, null, options); 
     return bmImg; 
    } catch (Exception e) { 
     e.printStackTrace(); 
     return null; 
    } 
}` 

什么可以是问题?

回答

0
private Bitmap loadBitmap() { 
    Bitmap bmQR = null; 
    InputStream inputStream = null; 

    try { 
     inputStream = OpenHttpConnection(url); 
     bmQR = BitmapFactory.decodeStream(inputStream); 
     inputStream.close(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return bmQR; 
} 

private InputStream OpenHttpConnection(String strURL) throws IOException { 
    InputStream is = null; 
    URL url = new URL(strURL); 
    URLConnection urlConnection = url.openConnection(); 

    try { 
     HttpURLConnection httpConn = (HttpURLConnection) urlConnection; 
     httpConn.setRequestMethod("GET"); 
     httpConn.connect(); 

     if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) { 
      is = httpConn.getInputStream(); 
     } 
    } catch (Exception ex) { 
    } 
    return is; 
} 
+0

嗨吉利,请看看我发布这个问题时? – 2012-05-09 12:25:23

+0

@NandlalVirani - 上面的评论是否提到这个问题是两个月大的事实?你从来没有接受过这个问题的答案,所以让其他人继续回答它似乎并不奇怪。如果您自己确实找到答案,请考虑将其作为回答并接受,以便其他人不会尝试解决您已解决的问题。 – 2012-05-09 13:23:37

0

我只是使用这个,它的工作很好,在我的情况。

URL newurl; 
try 
{ 
newurl = new URL("http://www.russiawear.com/components/com_virtuemart/shop_image/product/youth_russia_usa_4e2f7f78b543c.jpg"); 
bitmap = BitmapFactory.decodeStream(newurl.openConnection().getInputStream()); 
} 
catch (IOException e) 
{ 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} 

更新:

代码从URL下载文件,

try 
    { 
     URL url = new URL("http://www.russiawear.com/components/com_virtuemart/shop_image/product/youth_russia_usa_4e2f7f78b543c.jpg"); //you can write here any link 
     File file = new File("/sdcard/temp.jpg"); 
     /* Open a connection to that URL. */ 
     URLConnection ucon = url.openConnection(); 

     /* 
     * Define InputStreams to read from the URLConnection. 
     */ 
     InputStream is = ucon.getInputStream(); 
     BufferedInputStream bis = new BufferedInputStream(is); 
     /* 
     * Read bytes to the Buffer until there is nothing more to read(-1). 
     */ 
     ByteArrayBuffer baf = new ByteArrayBuffer(50); 
     int current = 0; 
     while ((current = bis.read()) != -1) 
     { 
      baf.append((byte) current); 
     } 

     /* Convert the Bytes read to a String. */ 
     FileOutputStream fos = new FileOutputStream(file); 
     fos.write(baf.toByteArray()); 
     fos.close(); 
    } 
    catch (IOException e) 
    { 
     Log.d("ImageManager", "Error: " + e); 
    } 

然后使用文件创建位图,

bitmap = BitmapFactory.decodeFile("/sdcard/temp.jpg"); 
+0

我已经使用此代码尝试还但没有结果 – 2012-03-01 12:59:43

+0

是的....现在我发现问题...它太大的图像大小 – 2012-03-01 13:10:48

+0

如何在Android下载大图像? – 2012-03-01 13:11:25