在设置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小時熱門文章