Android内存不足位图

Android内存不足位图,android,memory,bitmap,Android,Memory,Bitmap,我的应用程序有问题: java.lang.OutOfMemoryError: Failed to allocate a 1136368 byte allocation with 127432 free bytes at dalvik.system.VMRuntime.newNonMovableArray(Native Method) at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)

我的应用程序有问题:

java.lang.OutOfMemoryError: Failed to allocate a 1136368 byte allocation with 127432 free bytes
        at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
        at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
        at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587)
        at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:422)
        at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
        at android.content.res.Resources.loadDrawable(Resources.java:2110)
        at android.content.res.Resources.getDrawable(Resources.java:700)
        at android.widget.ImageView.resolveUri(ImageView.java:636)
        at android.widget.ImageView.setImageResource(ImageView.java:366)
        at com.scoutapp.lite.ImageLoader.DisplayImage(ImageLoader.java:40)
        at com.scoutapp.lite.CustomArrayAdapter.getView(CustomArrayAdapter.java:49)
        at android.widget.AbsListView.obtainView(AbsListView.java:2242)
        at android.widget.ListView.makeAndAddView(ListView.java:1790)
        at android.widget.ListView.fillDown(ListView.java:691)
        at android.widget.ListView.fillGap(ListView.java:655)
        at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5148)
        at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:4259)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
        at android.view.Choreographer.doCallbacks(Choreographer.java:574)
        at android.view.Choreographer.doFrame(Choreographer.java:543)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5019)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
基本上我有一个活动,其中有一个带有一些图像的列表。它们加载正确,但当我向下滚动时,我有一个力关闭此错误。我在这里发布了我的两个课程:

public class CustomArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
private final String[] img;
public ImageLoader imageLoader;

String t;

public CustomArrayAdapter(Context context, String[] values,String[]img) {
    super(context, R.layout.activity_spec, values);
    this.context = context;
    this.values = values;
    this.img=img;
    imageLoader=new ImageLoader(getContext().getApplicationContext());
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.list_row, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.title);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.list_image);
    textView.setText(values[position]);

    // Change icon based on name
    String s = values[position];
//        System.out.println(t);
    //Log.d(t,img[position]);
    if(img==null){

    }else{

    int resourceId = context.getResources().getIdentifier(img[position],"drawable", context.getPackageName());
   // int drawableId = Integer.parseInt(img[position]);
        Bitmap bm;
      //  bm = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(filepath),100, 100, true);
        //mPicture = new ImageView(context);
        //mPicture.setImageBitmap(bm);
        imageLoader.DisplayImage(resourceId, imageView);
    //imageView.setImageResource(resourceId);

            //img.recycle();
            //img = null;

    }
   /* if (s.equals("Lupetti")) {
        imageView.setImageResource(R.drawable.lupetti1);
    } else if (s.equals("Reparto")) {
        imageView.setImageResource(R.drawable.reparto1);
    } else if (s.equals("Clan")) {
        imageView.setImageResource(R.drawable.clan1);
    }
*/
    return rowView;
}
}
公共类CustomArrayAdapter扩展了ArrayAdapter{
私人最终语境;
私有最终字符串[]值;
私有最终字符串[]img;
公共图像加载器;
字符串t;
公共CustomArrayAdapter(上下文上下文,字符串[]值,字符串[]img){
super(上下文、R.layout.activity_spec、值);
this.context=上下文;
这个值=值;
这个。img=img;
imageLoader=新的imageLoader(getContext().getApplicationContext());
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
LayoutFlater充气器=(LayoutFlater)上下文
.getSystemService(上下文布局\充气机\服务);
视图行视图=充气机。充气(R.layout.list\u行,父级,false);
TextView TextView=(TextView)rowView.findViewById(R.id.title);
ImageView ImageView=(ImageView)rowView.findViewById(R.id.list\u image);
setText(值[位置]);
//根据名称更改图标
字符串s=值[位置];
//系统输出打印ln(t);
//对数d(t,img[位置]);
如果(img==null){
}否则{
int resourceId=context.getResources().getIdentifier(img[position],“drawable”,context.getPackageName());
//int drawableId=Integer.parseInt(img[position]);
位图bm;
//bm=Bitmap.createScaledBitmap(BitmapFactory.decodeFile(filepath),100100,true);
//mPicture=新图像视图(上下文);
//mPicture.setImageBitmap(bm);
DisplayImage(resourceId,imageView);
//setImageResource(resourceId);
//img.recycle();
//img=null;
}
/*如果(s.equals(“Lupetti”)){
setImageResource(R.drawable.lupetti1);
}否则,如果(s.equals(“Reparto”)){
setImageResource(R.drawable.reparto1);
}否则,如果(s.equals(“宗族”)){
setImageResource(R.drawable.clan1);
}
*/
返回行视图;
}
}
以及:

公共类ImageLoader{
MemoryCache MemoryCache=新的MemoryCache();
文件缓存文件缓存;
private Map ImageView=Collections.synchronizedMap(新的WeakHashMap());
执行服务执行服务;
公共图像加载器(上下文){
fileCache=新的fileCache(上下文);
executorService=Executors.newFixedThreadPool(5);
}
最终int存根\u id=R.drawable.ic\u存根;
public void DisplayImage(int-url,ImageView-ImageView)
{
imageView.put(imageView,“url”);
//位图位图=memoryCache.get(url);
如果(url!=0)
setImageResource(url);
其他的
{
队列照片(“url”,图像视图);
setImageResource(存根id);
}
}
私有void队列照片(字符串url,ImageView)
{
PhotoToLoad p=新的PhotoToLoad(url,imageView);
executorService.submit(新的PhotoLoader(p));
}
私有位图getBitmap(字符串url)
{
文件f=fileCache.getFile(url);
//从SD缓存
位图b=解码文件(f);
如果(b!=null)
返回b;
//从网络
试一试{
位图=空;
URL imageUrl=新URL(URL);
HttpURLConnection conn=(HttpURLConnection)imageUrl.openConnection();
连接设置连接超时(30000);
连接设置读取超时(30000);
conn.setInstanceFollowRedirects(真);
InputStream is=conn.getInputStream();
OutputStream os=新文件OutputStream(f);
Utils.CopyStream(is,os);
os.close();
位图=解码文件(f);
返回位图;
}捕获(例外情况除外){
例如printStackTrace();
返回null;
}
}
//对图像进行解码和缩放以减少内存消耗
私有位图解码文件(文件f){
试一试{
//解码图像大小
BitmapFactory.Options o=新的BitmapFactory.Options();
o、 inJustDecodeBounds=true;
解码流(新的FileInputStream(f),null,o);
//找到正确的刻度值。它应该是2的幂。
所需的最终int_尺寸=70;
内部宽度=o.向外宽度,高度=o.向外高度;
int标度=1;
while(true){

如果(width_tmp/2android中的位图可能会有很多问题,但首先你的适配器实现非常糟糕,你不会重复使用列表行,我相信这是相当滞后的,这可能是你问题的根源

您首先需要阅读convertView模式,使用它,然后告诉我们问题是否仍然存在

更新:

首先,您不需要每次调用
getView()
时都使布局更平坦,您可以在适配器构造函数中设置它,然后重用它

下一个listView有一种机制,可以只创建屏幕上可见的行数,当一行离开屏幕时,就可以重用它。您需要做的是检查是否必须从头创建新行或重用现有行并用新数据填充它

public class CustomArrayAdapter extends ArrayAdapter<String> {

    // Variable declarations
    LayoutInflater inflater;

    public CustomArrayAdapter(Context context, String[] values,String[]img) {
        // your code
        this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

         // convertView is null when new row is needed
         View rowView = convertView;

         if(rowView == null) {
             // new row is needed to inflate new row
             rowView = inflater.inflate(R.layout.list_row, parent, false);
         }


         TextView textView = (TextView) rowView.findViewById(R.id.title);
         ImageView imageView = (ImageView) rowView.findViewById(R.id.list_image);
         textView.setText(values[position]);

         // rest of your code
         return rowView;
     }
 }
公共类CustomArrayAdapter扩展了ArrayAdapter{
//变量声明
充气机;
公共CustomArrayAdapter(上下文上下文,字符串[]值,字符串[]img){
//你的代码
this.inflater=(LayoutInflater)context.getSystemService(context.LAYOUT\u inflater\u SERVICE);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
//需要新行时,convertView为空
视图行视图=转换视图;
if(rowView==null){
//需要新的行来为新的行充气
rowView=充气机。充气(R.layout.list\u行,父项,false);
}
TextView TextView=(TextView)rowView.findViewById(R.id.title);
ImageView ImageView=(ImageView)rowView.findViewById(R.id.list\u image);
setText(值[位置]);
//代码的其余部分
public class CustomArrayAdapter extends ArrayAdapter<String> {

    // Variable declarations
    LayoutInflater inflater;

    public CustomArrayAdapter(Context context, String[] values,String[]img) {
        // your code
        this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

         // convertView is null when new row is needed
         View rowView = convertView;

         if(rowView == null) {
             // new row is needed to inflate new row
             rowView = inflater.inflate(R.layout.list_row, parent, false);
         }


         TextView textView = (TextView) rowView.findViewById(R.id.title);
         ImageView imageView = (ImageView) rowView.findViewById(R.id.list_image);
         textView.setText(values[position]);

         // rest of your code
         return rowView;
     }
 }