Android
发布时间:2022-04-28 发布网站:大佬教程 code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Android 图片处理避免出现oom的方法详解,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
1. 通过设置采样率压缩
res资源图片压缩 decoderesource
@H_
772_7@
public Bitmap decodeSampledBitmapFrom
resource(
resources res,int resId,int reqWidth,int reqHeight)
{
// First decode with inJustDecodeBounds=true to
check dimensions
final BitmapFactory
.options options = new BitmapFactory
.options(
);
option
s.inJustDecodeBounds = true;
BitmapFactory.decode
resource(res,resId,options
);
// Calculate inSampleSize
option
s.inSampleSize = calculateInSampleSize(options,reqWidth,reqHeight
);
// Decode bitmap with inSampleSize set
option
s.inJustDecodeBounds =
false;
return BitmapFactory.decode
resource(res,options
);
}
uri图片压缩 decodeStream
@H_
772_7@
public Bitmap decodeSampledBitmapFromUri(Uri uri,int reqHeight)
{
Bitmap bitmap = null;
try
{
BitmapFactory
.options options = new BitmapFactory
.options(
);
option
s.inJustDecodeBounds = true;
BitmapFactory.decodeStream(getContentResolver()
.openInputStream(uri),null,options
);
option
s.inSampleSize = BitmapUtil
s.calculateInSampleSize(options,UtilUnitConversion.dip2px(MyApplication.mCo
ntext,reqWidth),reqHeight)
);
option
s.inJustDecodeBounds =
false;
bitmap = BitmapFactory.decodeStream(getContentResolver()
.openInputStream(uri),options
);
} catch (Exception
E) {
e.printStackTrace(
);
}
return bitmap;
}
本地File url图片压缩
@H_
772_7@
public static Bitmap getloadlBitmap(
String load_url,int width,int height)
{
Bitmap bitmap = null;
if (!UtilText.isEmpty(load_url))
{
File file = new File(load_url
);
if (file.exists())
{
FileInputStream fs = null;
try
{
fs = new FileInputStream(fil
E);
} catch (FileNotFoundException
E) {
e.printStackTrace(
);
}
if (null
!= fs)
{
try
{
BitmapFactory
.options opts = new BitmapFactory
.options(
);
opt
s.inJustDecodeBounds = true;
BitmapFactory.decodeFileDescriptor(f
s.getFD(),opts
);
opt
s.inDither =
false;
opt
s.inPurgeable = true;
opt
s.inInputShareable = true;
opt
s.inTempStorage = new byte
[32 * 1024];
opt
s.inSampleSize = BitmapUtil
s.calculateInSampleSize(opts,width),height)
);
opt
s.inJustDecodeBounds =
false;
bitmap = BitmapFactory.decodeFileDescriptor(f
s.getFD(),opts
);
} catch (IOException
E) {
e.printStackTrace(
);
} finally
{
if (null
!= fs)
{
try
{
f
s.close(
);
} catch (IOException
E) {
e.printStackTrace(
);
}
}
}
}
}
}
return bitmap;
}
根据显示的图片大小进行SampleSize的计算
@H_
772_7@
publi
c int calculateInSampleSize(BitmapFactory
.options options,int reqHeight)
{
if (reqWidth == 0 || reqHeight == 0)
{
return 1;
}
// Raw height and width of image
final int height = option
s.outHeight;
final int width = option
s.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth)
{
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and
// keeps both height and width larger than the
requested height and width.
while ((halfHeight / inSampleSiz
E) >= reqHeight && (halfWidth / inSampleSiz
E) >= reqWidth)
{
inSampleSize *= 2;
}
}
return inSampleSize;
}
调用方式:
@H_403_31@复制代码 代码如下:
大佬总结
以上是大佬教程为你收集整理的Android 图片处理避免出现oom的方法详解全部内容,希望文章能够帮你解决Android 图片处理避免出现oom的方法详解所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。