2014-09-02 80 views
1

我需要將高分辨率的遠程圖像加載到imageview,但不使用任何外部庫或框架。圖片就像是2150x1500像素,這裏是我的加載代碼:將大於2048x2048的圖像加載到Android上的ImageView上

   URL imageUrl = new URL(url); 

       HttpURLConnection connection; 
       if (url.startsWith("https://")) { 
        connection = (HttpURLConnection) imageUrl.openConnection(); 
       } else { 
        connection = (HttpURLConnection) imageUrl.openConnection(); 
       } 
       connection.setConnectTimeout(30000); 
       connection.setReadTimeout(30000); 
       connection.setInstanceFollowRedirects(true); 

       InputStream is = connection.getInputStream();        
       OutputStream os = new FileOutputStream(f); 
       CacheUtils.CopyStream(is, os); 
       os.close(); 
       image = decodeFile(f); 
       img.setImageBitmap(image); 

這裏是decodeFile功能:

return BitmapFactory.decodeStream(new FileInputStream(f), null, o2); 

,我總是得到紋理尺寸過大的異常。有什麼方法可以加載這些圖片嗎?或者有沒有辦法調整圖像大小不是2或4倍,而是調整大小以適應寬度2048像素?

回答

0
class DownloadImage extends AsyncTask<String, Void, Bitmap> 
{ 
    ImageView bmImage; 

    public DownloadImage(ImageView bmImage) { 
     this.bmImage = bmImage; 
    } 
    @Override 
    protected Bitmap doInBackground(String... urls) { 
     String urldisplay = urls[0]; 
     Bitmap mIcon11 = null; 
     try { 
      InputStream in = new java.net.URL(urldisplay).openStream(); 
      mIcon11 = BitmapFactory.decodeStream(in); 
     } catch (Exception e) { 
      Log.e("Error", e.getMessage()); 
      e.printStackTrace(); 
     } 
     return mIcon11; 
    } 
    @Override 
    protected void onPostExecute(Bitmap result) { 
     bmImage.setImageBitmap(Bitmap.createScaledBitmap(result, 120, 120, false)); 
    } 
} 

指定高度和寬度則需要120

調用此,而不是在你的OnCreate

new DownloadImage((ImageView) findViewById(R.id.imageView1)) 
       .execute("https://........."); 
+0

我試過了,但我離開的內存異常和應用程序崩潰(當我使用2048高度和寬度)。 – 2014-09-02 10:20:22

+0

你可以給我的圖片網址嗎? – 2014-09-02 10:33:07

+0

http://youngsday.com/wp-content/uploads/2014/04/new-york.jpg – 2014-09-02 10:42:52

相關問題