2011-10-14 16 views
2

私は与えられたURLから画像をダウンロードしてImageViewの中に表示する方法を知っています。 manifest.xmlファイルに記載する必要があるのはpermissionsですか?アプリケーションのURLから画像をダウンロードする方法

+0

これを簡単に実行する必要があります。 http://developer.android.com/guide/topics/fundamentals/processes-and-threads.html#WorkerThreads –

+0

初心者のためのこの素敵なチュートリアルをチェックしてください:[Webへの接続:AndroidでのI/Oプログラミング](http: /www.devx.com/wireless/Article/39810/1954)。 –

答えて

6

あなたはこのコードを試すことができ、インターネット

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

にアクセスするには、このpermissionを配置する必要があります。

String imageurl = "YOUR URL"; 
InputStream in = null; 

try 
{ 
    Log.i("URL", imageurl); 
    URL url = new URL(imageurl); 
    URLConnection urlConn = url.openConnection(); 
    HttpURLConnection httpConn = (HttpURLConnection) urlConn; 
    httpConn.connect(); 

    in = httpConn.getInputStream(); 
} 
catch (MalformedURLException e) 
{ 
    e.printStackTrace(); 
} 
catch (IOException e) 
{ 
    e.printStackTrace(); 
} 
Bitmap bmpimg = BitmapFactory.decodeStream(in); 
ImageView iv = "YOUR IMAGE VIEW"; 
iv.setImageBitmap(bmpimg); 
2

あなたは、AndroidマニフェストファイルにINTERNETusage許可を設定し、URLをrequestするjava.net.URLjava.net.URLConnectionクラスを使用する必要があります。

0

これを行うことができるBitmapFactoryクラスがあります。 InputStreamからBitmapオブジェクトを作成し、ImageViewに表示することができます。

通常、URLConnectionを使用してURLオブジェクトのInputStreamを取得することは、ビットマップファクトリでは常に機能しないことにご注意ください。回避策はここに提示されていますAndroid: Bug with ThreadSafeClientConnManager downloading images

3

バックグラウンドスレッドを使用してイメージを取得し、イメージを取得した後、イメージビューでhanndlerを使用して設定します。

new Thread(){ 
    public void run() { 
     try { 
      Bitmap bitmap = BitmapFactory.decodeStream(new URL("http://imageurl").openStream()); 
      Message msg = new Message(); 
      msg.obj = bitmap; 
      imageHandler.sendMessage(msg); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
}.start(); 

ダウンロードした画像をimgaeviewで設定するハンドラコードです。

Handler imageHandler = new Handler(){ 
    public void handleMessage(Message msg) { 
     if(msg.obj!=null && msg.obj instanceof Bitmap){ 
      imageview.setBackgroundDrawable(new BitmapDrawable((Bitmap)msg.obj)); 
     } 

    }; 
}; 

インターネットアクセス権が必要です。

オプションA:

public static Bitmap getBitmap(String url) { 
    Bitmap bm = null; 
    try { 
     URL aURL = new URL(url); 
     URLConnection conn = aURL.openConnection(); 
     conn.connect(); 
     InputStream is = conn.getInputStream(); 
     BufferedInputStream bis = new BufferedInputStream(is); 
     bm = BitmapFactory.decodeStream(new FlushedInputStream(is)); 
     bis.close(); 
     is.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return bm; 
} 

オプションB:この

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

外観は

public Bitmap getBitmapFromURL(String src) { 
    try { 
     URL url = new URL(src); 
     HttpURLConnection connection = (HttpURLConnection) url 
       .openConnection(); 
     connection.setDoInput(true); 
     connection.connect(); 
     InputStream input = connection.getInputStream(); 
     Bitmap myBitmap = BitmapFactory.decodeStream(input); 
     input.close(); 
     return myBitmap; 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return null; 
    } 
} 

強行は、バックグラウンドスレッドでメソッドを実行します。

関連する問題