2011-08-08 62 views
0

即時通訊嘗試在我的應用程序中顯示來自url的圖像。但是我使用的方法很長。 這個代碼我建立在計算器在10-12秒加載從URL到可繪製或位圖的圖像:最好和最快的方式

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);} 任何能告訴如何最好的和緊固顯示圖像在我的應用程序? 問候,彼得。

回答

1

你不能廢除下載和解碼圖像所需的時間。數字'10'只是圖像質量的函數,您只能嘗試優化此數字。

如果服務器由您管理,您可能需要花一些時間根據您的UI要求優化可下載圖像的大小。也請嘗試延遲加載(我希望你不在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; 
} 
相關問題