[英]Scaling down Images in android
I am implementing an application in which an activity shows a image view with full screen. 我正在实现一个应用程序,其中活动显示全屏图像视图。 Now i have some images downloaded in application's file directory.
现在,我在应用程序的文件目录中下载了一些图像。 I want to set image to image view in that activity for which i am scaling down the larger images.
我想在该活动中将图像设置为图像视图,为此我正在缩小较大的图像。 I am getting OutOfMemory error after 10 to 15 mins of using application.
使用应用程序10到15分钟后,我收到OutOfMemory错误。 error is in decodeFile of BitmapFactory.
错误在BitmapFactory的解码文件中。 Here is my code :
这是我的代码:
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float)height / (float)reqHeight);
} else {
inSampleSize = Math.round((float)width / (float)reqWidth);
}
}
return inSampleSize;
}
public static Bitmap decodeSampledBitmapFromFile(String imagePath,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(imagePath, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(imagePath, options);
}
Right now i am giving reqHeight = 500 and reqWidth = 500 现在我给reqHeight = 500和reqWidth = 500
This code is given in Android guidelines page. 此代码在Android准则页面中给出。 Is there anything i am missing, if so please help me out.
我有什么想念的吗,如果是的话,请帮帮我。 I tried lots of things but not getting any solution.
我尝试了很多事情,但没有得到任何解决方案。
Change you int inSampleSize = 1; 更改int inSampleSize = 1; to int inSampleSize = 2 or 3 and test the application again, I hope this will solve your problem.
int inSampleSize = 2或3并再次测试该应用程序,希望这可以解决您的问题。 I also suggest insted of this technique,try lazy loading for all you image dowanload by doing so there will be no bitmap and outOfMemoryError.
我还建议安装这种技术,尝试对所有图像加载延迟加载,这样就不会出现位图和outOfMemoryError。 Yo can have lazy loading - https://github.com/thest1/LazyList
哟可以延迟加载-https: //github.com/thest1/LazyList
Try using this 试试这个
Bitmap resizedbitmap2 = Bitmap.createScaledBitmap(your_bitmap, width, height,
true);
it will help you. 它会帮助你。
There might be a memory leak in your app if you are storing list of bitmaps returned by decodeSampledBitmapFromFile somewhere. 如果您将某个地方的数据存储在decodeSampledBitmapFromFile返回的位图列表中,则应用程序中可能会发生内存泄漏。 Try to check for memory leak and also use Bitmap.recycle() to recycle bitmaps and help android free memory.
尝试检查内存泄漏,并使用Bitmap.recycle()回收位图并帮助android释放内存。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.