在設置ImageView資源的時候,這時的圖片是來自SD卡,查看API很容易就會看到view.setImageUri(Uri u)這個函數。所以一般會這樣寫:
ImageView view = (ImageView)findViewById(...);
File file = new File(path);
Uri uri = Uri.from(file);
view.setImageUri(uri);
但是這樣做是不行的,因爲setImageUri這個函數使不能讀取SD卡中的文件的,只能讀取手機本身的文件。
所以改用以下這種方式:
Bitmap bit = BitmapFactory.decodeFile("/sdcard/image/test.jpg"); //自定義//路徑
iv.setImageBitmap(bit);
用這種方式的時候,如果圖片太大,很可能會出現這樣的bug:
Java.lang.OutOfMemoryError : bitmap size exceeds VM budget
解決方法是:
FileInputStream f = new FileInputStream(path);
Bitmap bm = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;//圖片的長寬都是原來的1/8
BufferedInputStream bis = new BufferedInputStream(f);
bm = BitmapFactory.decodeStream(bis, null, options);
view.setImageBitmap(bm);
ImageView設置成手機內存卡圖片
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章
Android開發之ButterKnife,GreenDao簡單運用,以及推薦並使用一個好用的開源庫BaseRecyclerViewAdapterHelper。
不要说话丶
2018-08-27 20:04:11
nexus 5X update 8.0
杨飞扬
2018-08-27 20:04:03
Retrofit2 使用心得
杨飞扬
2018-08-27 20:03:51
Android studio Errors
杨飞扬
2018-08-27 20:03:48
Summary of problems when coding
杨飞扬
2018-08-27 20:03:48
Android Studio 配置Android Annotation
杨飞扬
2018-08-27 20:03:48
Android內存管理機制
叫我流氓
2018-08-27 20:03:47
2018 Android面試心得,已拿到offer
huison_ma
2018-08-27 20:03:45
Android中的dispatchTouchEvent()、onInterceptTouchEvent()和onTouchEvent()
叫我流氓
2018-08-27 20:03:44
Android滑動衝突解決方式(下拉刷新上拉加載更多,適配RecyclerView/ListView/ScrollView)
huison_ma
2018-08-27 20:03:44
Activity生命週期基礎
叫我流氓
2018-08-27 20:03:44
短信監聽---數據庫變動
叫我流氓
2018-08-27 20:03:44
Android應用程序框架四大重要的組件
叫我流氓
2018-08-27 20:03:44
Android studio 打包jar包
叫我流氓
2018-08-27 20:03:44
SQLite---T_double
叫我流氓
2018-08-27 20:03:43
24小時熱門文章