安卓圖片縮放中java.lang.IllegalArgumentException: width and height must be > 0錯誤

來源:互聯網
上載者:User

標籤:

  第一次寫部落格,都是低級的錯誤,大神見笑了。

  今天做安卓載入圖片到 ImageView,報了 一個 java.lang.IllegalArgumentException: width and height must be > 0錯誤,只能怪我英文不好,沒看懂。先把My Code貼出來。

 1 bmp = BitmapFactory.decodeStream(cr.openInputStream(uri)); 2  3 // 縮放圖片 4 WindowManager wm = getWindowManager(); 5 int width = wm.getDefaultDisplay().getWidth(); 6 int height = wm.getDefaultDisplay().getHeight(); 7 // 圖片的寬高 8 int photo_width = bmp.getWidth(); 9 int photo_height = bmp.getHeight();10 // 縮放比例,如小於1就設定為111 int bili = (photo_width/width)>(photo_height/height)?(photo_width/width):(photo_height/height);12 bili = bili>1?bili:1;13 System.out.println("bili:"+bili);14 Matrix matrix = new Matrix(); 15 matrix.postScale(1/bili,1/bili); //長和寬放大縮小的比例16 Bitmap resizeBmp = Bitmap.createBitmap(bmp,0,0,bmp.getWidth(),bmp.getHeight(),matrix,true);17 18 System.out.println(resizeBmp.getWidth()+":"+resizeBmp.getHeight());19 20 img1.setImageBitmap(resizeBmp);

一直在15行報錯,我就不明白了,最後百度 matrix.postScale 中出現了賦值,於是我恍然大悟, matrix.postScale 接收的浮點類型的數,而我給的整數類型, 於是我該成了 matrix.postScale(1f/bili,1f/bili); 就沒報錯了。後來我仔細想想,這裡的計算不合理,前面計算比例的時候應該用float的。

  以後還是要養成好習慣,浮點類型的盡量寫上 f。

 

安卓圖片縮放中java.lang.IllegalArgumentException: width and height must be > 0錯誤

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.