简体   繁体   English

在Android Studio中运行项目时出现内存不足错误

[英]Out of Memory error while running project in android studio

I am getting Out of memory error while running my project. 我在运行项目时遇到内存不足错误。 I understand this error because of large background images in the project. 我理解此错误是由于项目中的背景图片较大。 I came across some articles to scale down an image and then load them. 我遇到了一些文章,以按比例缩小图像,然后加载它们。 How can I scale down all the images and load them efficiently in the project. 如何缩小所有图像并将其有效加载到项目中。

Picasso is a great library for handling images for Android. 毕加索(Picasso)是一个出色的库,用于处理Android图像。

http://square.github.io/picasso/ http://square.github.io/picasso/

Loading an image for a specific size and can easy like this: 加载特定大小的图像,可以很容易地像这样:

Picasso.with(context)
  .load(url)
  .resize(50, 50)
  .centerCrop()
  .into(imageView)

If you are truly showing alot of images, consider setting android:hardwareAccelerated="true" 如果您确实要显示大量图像,请考虑设置android:hardwareAccelerated =“ true”

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="foo.bar"
    android:hardwareAccelerated="true">
...
</manifest>

To reduce the size of images change the inSampleSize of each image. 要减小图像的大小,请更改每个图像的inSampleSize。 If inSampleSize is set to a value > 1 then the decoder subsamples the original image and returns you a smaller image to save memory. 如果inSampleSize设置为大于1的值,则解码器会对原始图像进行二次采样,然后返回较小的图像以节省内存。 inSampleSize can be found in BitmapFactory.Options inSampleSize可以在BitmapFactory.Options中找到

Go to the following link for more details on this :- 转到以下链接以获取有关此内容的更多详细信息:

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

If your images still lags you can scale them down further by help of Bitmap.createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter) method. 如果图像仍然滞后,则可以借助Bitmap.createScaledBitmap(Bitmap src,int dstWidth,int dstHeight,boolean filter)方法进一步缩小图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM