2012-03-01 3 views
0

このメソッドを呼び出すと、私はいつも応答しますが、ブラウザで実行したとしたら、画像を表示します。 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

こんにちはギリ、 –

+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

大きな画像はどのようにしてアンドロイドにダウンロードできますか? –

関連する問題