java android图像视图内存不足错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12250300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
android Image view out of memory error
提问by Hayk Nahapetyan
In my Android project I have imageButton , and after clicking on it , it must open new Activity with imageView , and in my new Activity I must see the ImageButton's image only in large type , my image size is 17mb , and I got out of memory error. But my code works for images with little size. Can somebody help to resize image or change some bitmap options or advice other way? I'm new in android , and sorry for bad English :)
在我的 Android 项目中,我有 imageButton ,单击它后,它必须使用 imageView 打开新的 Activity,在我的新 Activity 中,我必须只看到大字体的 ImageButton 图像,我的图像大小为 17mb ,并且内存不足错误。但我的代码适用于小尺寸的图像。有人可以帮助调整图像大小或更改一些位图选项或以其他方式提出建议吗?我是 android 新手,抱歉英语不好:)
Here is my new Activity's XML
这是我的新活动的 XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/LL11"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Here must be an image">
</TextView>
<ImageView
android:id="@+id/imageView1"
android:maxWidth="10px"
android:maxHeight="10px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_action_search" />
</LinearLayout>
and the java code
和java代码
package com.example.example;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.ImageView;
package com.example.example;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.ImageView;
public class ActivityTwo extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_dialog);
Bundle extras = getIntent().getExtras();
String path = extras.getString("path");
if(null != path)
{
Uri myUri = Uri.parse(path);
super.onCreate(savedInstanceState);
ImageView img1 = (ImageView) findViewById(R.id.imageView1);
img1.setImageURI(myUri);
}
}
}
回答by
in your code start at if(null != path)
change to this
在您的代码中开始if(null != path)
更改为此
int size = 10; //minimize as much as you want
if(path != null){
Bitmap bitmapOriginal = BitmapFactory.decodeFile(pathath);
Bitmap bitmapsimplesize = Bitmap.createScaledBitmap(bitmapOriginal,bitmapOriginal.getWidth() / size, bitmapOriginal.getHeight() / size, true);
bitmapOriginal.recycle();
img1.setImageBitmap(bitmapsimplesize);
}
回答by Victor de Francisco Domingo
I have had some problems with Images
and OutOfMemory
exceptions, and all of them are obviously caused by the fact that I use too much memory that is assigned for the app (called heap).
我遇到了一些问题Images
和OutOfMemory
异常,所有这些显然都是由于我使用了太多分配给应用程序的内存(称为堆)造成的。
Like I can see in your code, you create an image every time you push the button, and like you said, if the Image has 17M
of size, probably if your device is low quality, it has 20M
of max heap for each app, so you are out of memory with two images.
就像我在你的代码中看到的那样,你每次按下按钮都会创建一个图像,就像你说的,如果图像有一定17M
的尺寸,可能如果你的设备质量低,它有20M
每个应用程序的最大堆,所以你两个图像内存不足。
Maybe you can create an image only one time, if you must create more than one image, try to remove previous images, and try to call System.gc()
, it could be helpful.
也许您只能创建一次图像,如果您必须创建多个图像,请尝试删除以前的图像,并尝试调用System.gc()
,这可能会有所帮助。
If you really need to create more than one image, more than one time, you can build a reduced instance of image, setting inSampleSize
option before you create the image. If you put a value of 2
to this attribute, you will get a 1/2
image of the original, with reduced quality and size.
如果你真的需要创建多个图像,不止一次,你可以构建一个缩小的图像实例,inSampleSize
在创建图像之前设置选项。如果2
将此属性设置为 的值,您将获得1/2
原始图像,但质量和尺寸均有所降低。
Something like this:
像这样的东西:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2; // Or other value that you estimate
Then create the image with these options.
然后使用这些选项创建图像。
PS:It's not necessary to call super.onCreate()
more than once.
PS:没有必要super.onCreate()
多次调用。
回答by Hardik Nadiyapara
below code can help you for resize an Image
下面的代码可以帮助您调整图像大小
File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
File output = new File(dir, "image.png");
path = output.getAbsolutePath();
Bitmap b = BitmapFactory.decodeFile(cPath);
Bitmap out = Bitmap.createScaledBitmap(b, 320, 480, false);
FileOutputStream fout;
try{
fout = new FileOutputStream(output);
out.compress(Bitmap.CompressFormat.PNG, 100, fout);
fout.flush();
fout.close();
b.recycle();
out.recycle();
}catch(Exception e){
e.printStackTrace();
}
回答by Azadi B.
I realize this is an old thread, but since I just came across this error today myself, and found that I received the same error, but with a different reason, I wanted to post a different perspective on the matter.
我意识到这是一个旧线程,但由于我今天自己刚刚遇到了这个错误,并发现我收到了同样的错误,但出于不同的原因,我想就此事发表不同的看法。
In my case, this was an issue of the dimensions being too large, and not with the size (200K). Upon resizing to a smaller size (640px x 480px) the problem was resolved.
就我而言,这是尺寸太大的问题,而不是尺寸 (200K)。调整到较小的尺寸(640 像素 x 480 像素)后,问题就解决了。
Hope this can help someone else in the future who comes across this post.
希望这可以帮助将来遇到此帖子的其他人。
回答by Dulanga
Above solutions not works in my case, so I found this it work for me Try this one.
以上解决方案在我的情况下不起作用,所以我发现这对我有用 试试这个。
private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException {
BitmapFactory.Options op = new BitmapFactory.Options();
op.inJustDecodeBounds = true;
BitmapFactory.decodeStream(
getActivity().getContentResolver().openInputStream(selectedImage), null, op);
final int REQUIRED_SIZE = 1000;
int width_tmp = op.outWidth, height_tmp = op.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) {
break;
}
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
BitmapFactory.Options op2 = new BitmapFactory.Options();
op2.inSampleSize = scale;
return BitmapFactory.decodeStream(
getActivity().getContentResolver().openInputStream(selectedImage), null, op2);
}
Then set returned Bitmap
in to your imageView
like below
然后设置返回Bitmap
到你imageView
喜欢的下面
yourImageView.setImageBitmap(returnedBitmap);