讨教个android调用相册的问题

Android 码拜 8年前 (2016-03-30) 1008次浏览
使用测试机为:红米2 ,1g内存,miui7.2
问题描述:使用android打开相册功能,打开相册后选择一个图片后,返回时debug失效并跳出当前activity。
内存检测:打开相册选取图片之前  系统分配给应用的内存为 96M,应用已占用内存75M(怀疑是内存溢出)
代码:
打开相册:
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
“image/*”);
startActivityForResult(intent, PHOTO_PICK);
选取图片后的返回代码
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Uri uri = data.getData();
LogUtils.e(“URI–“+uri);
Bitmap bitmap = null;
if(uri!=null) {
BitmapFactory.Options options = new BitmapFactory.Options();
// 设置options.inJustDecodeBounds为true
options.inJustDecodeBounds = true;
// 节约内存
options.inPreferredConfig = Bitmap.Config.RGB_565; // 默认是Bitmap.Config.ARGB_8888
/* 下面两个字段需要组合使用 */
options.inPurgeable = true;
options.inInputShareable = true;
try {
//此时不会把图片读入内存,只会获取图片宽高等信息
bitmap = BitmapFactory.decodeStream(getContentResolver()
.openInputStream(uri), null, options);
int heitht = options.outHeight;
Log.i(“path”, “” + heitht);
// 根据需要设置压缩比例
int size = heitht / 800;
if (size <= 0) {
size = 1;
}
options.inSampleSize = size;
// 设置options.inJustDecodeBounds重新设置为false
options.inJustDecodeBounds = false;
//此时图片会按比例压缩后被载入内存中
bitmap = BitmapFactory.decodeStream(getContentResolver()
.openInputStream(uri), null, options);
LogUtils.e(“bitmap==” + bitmap);
CommonUtil.getMemory(“相册调取图片后内存”);
//ImageView imageView = (ImageView) findViewById(R.id.imageView1);
//imageView.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
LogUtils.e(“bitmap–“+bitmap);
}
解决方案

40

建议用第三方的图片加载功能。图片的OOM,你的这种方式只能减少,图片太大时,现在高清的图,好几M,还是不行的。

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明讨教个android调用相册的问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)