2014-09-02 78 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

相关问题