Android 图片加载时间
来源:互联网 发布:centos ntfs 编辑:程序博客网 时间:2024/06/06 23:16
图片加载时间测试:手机note3
获取图片地址
public void loadImage() { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("image/*"); startActivityForResult(intent, 1000); }
根据Uri获取图片的绝对地址
public String getAbsoluteImagePath(Uri uri) { // can post image String[] proj = { MediaStore.Images.Media.DATA }; Cursor cursor = getActivity().getContentResolver().query(uri, proj, // Which // columns // to // return null, // WHERE clause; which rows to return (all rows) null, // WHERE clause selection arguments (none) null); // Order-by clause (ascending by name) int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); }
根据绝对路径活取图片文件内容:
public byte[] readFile(String path) { long start = System.currentTimeMillis(); File f = new File(path); ByteArrayOutputStream bos = new ByteArrayOutputStream((int) f.length()); BufferedInputStream in = null; try { in = new BufferedInputStream(new FileInputStream(f)); int buf_size = 1024; byte[] buffer = new byte[buf_size]; int len = 0; while (-1 != (len = in.read(buffer, 0, buf_size))) { bos.write(buffer, 0, len); } long end = System.currentTimeMillis(); Log.d("PlaceholderFragment", "readFile " + path + " size=" + f.length() + " in time:" + (end - start) + "ms"); return bos.toByteArray(); } catch (IOException e) { e.printStackTrace(); } finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } return null; }
加载图片
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode != 1000) { Log.d("PlaceholderFragment", "this is not my request id!"); return; } if (resultCode != Activity.RESULT_OK) { Log.d("PlaceholderFragment", "resultCode =" + resultCode); return; } Uri originalUri = data.getData(); // 获得图片的uri String imagePath = getAbsoluteImagePath(originalUri); Log.d("PlaceholderFragment", "originalUri:" + originalUri.toString() + " imagePath:" + imagePath); byte[] imageData = readFile(imagePath); Bitmap bp = null; if (imageData != null) { long start = System.currentTimeMillis(); Options options = new Options(); options.inSampleSize = 10; bp = BitmapFactory.decodeByteArray(imageData, 0, imageData.length, options); long end = System.currentTimeMillis(); Log.d("PlaceholderFragment", "decodeByteArray bpgetAllocationByteCount(): " + bp.getByteCount() + " in time:" + (end - start) + "ms"); Log.d("PlaceholderFragment", "currentInfo: getHeight" + bp.getHeight() + " width:" + bp.getWidth() + " getScaledHeight(1):" + bp.getScaledHeight(1) + "bp.getScaledHeight(2)" + bp.getScaledHeight(2)); } if (bp != null) { ((ImageView) getView().findViewById(R.id.iv)).setImageBitmap(bp); } }运行结果:
D/PlaceholderFragment(4781): originalUri:content://media/external/images/media/54809 imagePath:/storage/emulated/0/DCIM/Camera/20150516_111556.jpg
D/PlaceholderFragment(4781): readFile /storage/emulated/0/DCIM/Camera/20150516_111556.jpg size=552204 in time:6ms
D/PlaceholderFragment(4781): decodeByteArray bpgetAllocationByteCount(): 147456 in time:22ms
D/PlaceholderFragment(4781): currentInfo: getHeight144 width:256 getScaledHeight(1):0bp.getScaledHeight(2)1
使用decodeFile加载:
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode != 1000) { Log.d("PlaceholderFragment", "this is not my request id!"); return; } if (resultCode != Activity.RESULT_OK) { Log.d("PlaceholderFragment", "resultCode =" + resultCode); return; } Uri originalUri = data.getData(); // 获得图片的uri String imagePath = getAbsoluteImagePath(originalUri); Log.d("PlaceholderFragment", "originalUri:" + originalUri.toString() + " imagePath:" + imagePath); long start = System.currentTimeMillis(); Options options = new Options(); options.inSampleSize = 10; Bitmap bp = null; bp = BitmapFactory.decodeFile(imagePath, options); long end = System.currentTimeMillis(); Log.d("PlaceholderFragment", "decodeByteArray bpgetAllocationByteCount(): " + bp.getByteCount() + " in time:" + (end - start) + "ms"); if (bp != null) { ((ImageView) getView().findViewById(R.id.iv)).setImageBitmap(bp); } }
运行结果:
D/PlaceholderFragment(3130): originalUri:content://media/external/images/media/54809 imagePath:/storage/emulated/0/DCIM/Camera/20150516_111556.jpg
D/PlaceholderFragment(3130): decodeByteArray bpgetAllocationByteCount(): 147456 in time:28ms
不适用option:
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode != 1000) { Log.d("PlaceholderFragment", "this is not my request id!"); return; } if (resultCode != Activity.RESULT_OK) { Log.d("PlaceholderFragment", "resultCode =" + resultCode); return; } Uri originalUri = data.getData(); // 获得图片的uri String imagePath = getAbsoluteImagePath(originalUri); Log.d("PlaceholderFragment", "originalUri:" + originalUri.toString() + " imagePath:" + imagePath); byte[] imageData = readFile(imagePath); Bitmap bp = null; if (imageData != null) { long start = System.currentTimeMillis(); bp = BitmapFactory.decodeByteArray(imageData, 0, imageData.length, null); long end = System.currentTimeMillis(); Log.d("PlaceholderFragment", "decodeByteArray bpgetAllocationByteCount(): " + bp.getByteCount() + " in time:" + (end - start) + "ms"); Log.d("PlaceholderFragment", "currentInfo: getHeight" + bp.getHeight() + " width:" + bp.getWidth() + " getScaledHeight(1):" + bp.getScaledHeight(1) + "bp.getScaledHeight(2)" + bp.getScaledHeight(2)); } if (bp != null) { ((ImageView) getView().findViewById(R.id.iv)).setImageBitmap(bp); } }结果:
500k的图片:
D/PlaceholderFragment(15244): readFile /storage/emulated/0/DCIM/Camera/20150516_111556.jpg size=552204 in time:4ms
D/dalvikvm(15244): GC_FOR_ALLOC freed 10316K, 45% free 24585K/44212K, paused 10ms, total 10ms
D/PlaceholderFragment(15244): decodeByteArray bpgetAllocationByteCount(): 9437184 in time:57ms
3M的图片:中间的gc次数更多
D/PlaceholderFragment(15244): readFile /storage/emulated/0/DCIM/Camera/20150516_111517.jpg size=3573513 in time:42ms
D/dalvikvm(15244): GC_FOR_ALLOC freed 10316K, 45% free 24585K/44212K, paused 10ms, total 10ms
D/dalvikvm(15244): GC_FOR_ALLOC freed 1K, 22% free 27544K/34992K, paused 11ms, total 11ms
I/dalvikvm-heap(15244): Grow heap (frag case) to 35.361MB for 3573529-byte allocation
D/dalvikvm(15244): GC_FOR_ALLOC freed 3499K, 29% free 27534K/38484K, paused 9ms, total 9ms
I/dalvikvm-heap(15244): Grow heap (frag case) to 80.696MB for 51121168-byte allocation
D/PlaceholderFragment(15244): decodeByteArray bpgetAllocationByteCount(): 51121152 in time:326ms
- Android 图片加载时间
- Android 图片异步加载 加载网络图片
- android加载网络图片,圆形图片加载
- android加载本地图片
- Android图片加载机制
- android 加载图片方式
- android异步加载图片
- android 异步加载图片
- android中加载图片
- android异步加载图片
- android异步加载图片
- android 图片加载进度条
- android加载网络图片
- android 加载图片方式
- Android 异步加载图片
- android 图片加载 方法
- Android 异步加载图片
- Android图片异步加载
- 和小强一起学数据结构之简单选择排序
- 初识ASP.NET MVC窗体验证与权限过滤---3.自定义过滤器验证Session超时
- 推箱子c++实现代码
- digest循环和$apply
- UIPickerView和UIDatePicker的常用方法
- Android 图片加载时间
- 更改Mysql数据库存放目录
- 排序算法—快速排序
- java 页面提交到后台是乱码问题
- 使用TortoiseGit在github上创建开源工程
- 第9周项目3-分数类中的运算符重载(续)
- matlab 直接修改fig图数据显示
- Qt 5.4.1学习(1)
- <div>中插入文本部分显示超出部分用省略号显示