2011-08-08 6 views
0

私のアプリでURLから画像を表示しようとしました。しかし、私が使用する方法は非常に長いです。 iは10-12秒にロードstackoverflowのurlからdrawableまたはbitmapへの画像:最高かつ最速の方法

public Bitmap getImage(String url,String src_name) throws java.net.MalformedURLException, java.io.IOException { 
      Bitmap bitmap; 
      HttpURLConnection connection = (HttpURLConnection)new URL(url) .openConnection(); 
      connection.setRequestProperty("User-agent","Mozilla/4.0"); 

      connection.connect(); 
      InputStream input= connection.getInputStream(); 

      bitmap = BitmapFactory.decodeStream(input); 

      return bitmap; 
} 

10画像について設立このコード。このコードを使用する場合。

///========================================================================================================================================== 
    public Drawable getImage(String url, String src_name) throws java.net.MalformedURLException, java.io.IOException 
     { 

     Drawable abc =Drawable.createFromStream(((java.io.InputStream)new java.net.URL(url).getContent()), src_name); 

      return abc; 


     } 

このコードを使用している場合 - 9-11秒にロードされた画像。 イメージは大きくありません。最大幅または高さは400〜450です。 ofcourseこのようなサイクルでこの機能を教えてください: for (int i =0;i<10;i++){image[i]=getImage(url);} 私のアプリでどのように最高とファーストショーのイメージを伝えることができますか? について、Peter。

答えて

1

イメージをダウンロードしてデコードするのに必要な時間を無駄にすることはできません。数字「10」は画像の品質に関する単なる関数であり、この数値で最適化を試みることができます。

あなたが管理しているサーバーの場合、UI要件に応じてダウンロード可能なイメージのサイズを最適化するために時間を費やしたい場合があります。また、lazy-loadingを試してみてください(UIスレッドでこれらの操作を実行しないことを祈っています)。レイジーダウンロードと怠惰な復号のための多くの解決策は、何度も議論されている:http://www.google.com.sg/search?q=android+images+lazy+load&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a

追記HttpURLConnectionの使用が推奨されていません。 HttpClientを使用してください。これはパフォーマンスにも影響する可能性があります。 http://lukencode.com/2010/04/27/calling-web-services-in-android-using-httpclient/

0
public static Bitmap getBitmapFromUrl(String url) { 
    Bitmap bitmap = null; 
    HttpGet httpRequest = null; 
    httpRequest = new HttpGet(url); 
    HttpClient httpclient = new DefaultHttpClient(); 

    HttpResponse response = null; 
    try { 
     response = (HttpResponse) httpclient.execute(httpRequest); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    if (response != null) { 
     HttpEntity entity = response.getEntity(); 
     BufferedHttpEntity bufHttpEntity = null; 
     try { 
      bufHttpEntity = new BufferedHttpEntity(entity); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     InputStream instream = null; 
     try { 
      instream = bufHttpEntity.getContent(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     bitmap = BitmapFactory.decodeStream(instream); 
    } 
    return bitmap; 
} 
0
public static Bitmap decodeFile(String filePath) { 
    // Decode image size 
    BitmapFactory.Options o = new BitmapFactory.Options(); 
    o.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(filePath, o); 

    // The new size we want to scale to 
    final int REQUIRED_SIZE = 1024; 

    // Find the correct scale value. It should be the power of 2. 
    int width_tmp = o.outWidth, height_tmp = o.outHeight; 
    int scale = 1; 
    while (true) { 
     if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE) 
      break; 
     width_tmp /= 2; 
     height_tmp /= 2; 
     scale *= 2; 
    } 

    // Decode with inSampleSize 
    BitmapFactory.Options o2 = new BitmapFactory.Options(); 
    o2.inSampleSize = scale; 
    Bitmap bitmap = BitmapFactory.decodeFile(filePath, o2); 
    return bitmap; 
} 
を見てみましょう
関連する問題