2012-03-01 4 views
0

이 메서드를 호출 할 때 항상 NULL이 반환됩니다. 브라우저에서 실행한다고 가정 할 경우 이미지를 표시합니다. public 비트 맵 getBitmapFromURL (String src) { Bitmap bmImg; URL myFileUrl = null;안드로이드에서 이미지를 다운로드 할 수 없습니다.

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

안녕하세요. Gili, 제발이 질문을 게시했을 때 보셨습니까? –

+0

@ NandlalVirani -이 질문은 2 개월 된 사실과 관련하여 위의 의견입니까? 그 질문에 대한 답을 결코 받아들이지 않았기 때문에 다른 사람들이 계속해서 대답하도록하는 것이 이상한 일은 아닙니다. 자신이 답을 찾은 경우 답을 게시하고 수락하여 다른 사용자가 이미 수정 한 문제를 해결하지 않도록 고려하십시오. –

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

나는 이미이 코드를 시도 파일을 사용하여 비트 맵을 만들 또한 하지만 결과가 없습니다 –

+0

예 .... 이제 문제가 발견되었습니다 ... 이미지의 크기가 너무 크다 –

+0

큰 이미지를 어떻게 안드로이드에 다운로드 할 수 있습니까? –

관련 문제