Android的图片查看内存不足的错误 [英] android Image view out of memory error

查看:184
本文介绍了Android的图片查看内存不足的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Andr​​oid项目中,我有ImageButton的,并点击它之后,就必须开辟新的活动与ImageView的,在我的新的活动,我必须看到的只是大类型的ImageButton的形象,我的图像大小为17MB,和我内存不足的错误。但我的code适用于几乎没有大小的图像。有人可以帮来调整图像或更改某些位图选项或建议其他的方式?我在新的Andr​​oid和对不起坏英语:)

下面是我的新活动的XML

 <?XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
 的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
 机器人:方向=垂直
 机器人:ID =@ + ID / LL11
 机器人:layout_width =WRAP_CONTENT
 机器人:layout_height =WRAP_CONTENT>
<的TextView
 机器人:ID =@ + ID / textView1
 机器人:layout_width =WRAP_CONTENT
 机器人:layout_height =WRAP_CONTENT
 机器人:文字=这里必须是一个图像>
< / TextView的>< ImageView的
    机器人:ID =@ + ID / imageView1
    安卓了maxWidth =10px的
    安卓了maxHeight =10px的
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentLeft =真
    机器人:layout_alignParentTop =真
    机器人:SRC =@绘制/ ic_action_search/>< / LinearLayout中>

和java的code

 包com.example.example;进口android.app.Activity;
进口android.net.Uri;
进口android.os.Bundle;
进口android.widget.ImageView;包com.example.example;进口android.app.Activity;
进口android.net.Uri;
进口android.os.Bundle;
进口android.widget.ImageView;公共类ActivityTwo延伸活动{
    @覆盖
      保护无效的onCreate(捆绑savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.custom_dialog);
        捆绑额外= getIntent()getExtras()。
        字符串路径= extras.getString(路径);
        如果(空!=路径)
        {
            乌里myUri = Uri.parse(路径);
            super.onCreate(savedInstanceState);
            ImageView的IMG1 =(ImageView的)findViewById(R.id.imageView1);
           img1.setImageURI(myUri);        }
    }
}


解决方案

在您的code开始在如果(空!=路径)更改此

  INT大小= 10; //减少多达你想
如果(路径!= NULL){
     位图bitmapOriginal = BitmapFactory.de codeFILE(pathath);
     位图bitmapsimplesize = Bitmap.createScaledBitmap(bitmapOriginal,bitmapOriginal.getWidth()/大小,bitmapOriginal.getHeight()/尺寸,真实);
     bitmapOriginal.recycle();
     img1.setImageBitmap(bitmapsimplesize);}

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 :)

Here is my new Activity's 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

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);

        }
    }
}

解决方案

in your code start at if(null != path) change to this

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);

}

这篇关于Android的图片查看内存不足的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆