【问题标题】:Display images to Images from file Paths从文件路径显示图像到图像
【发布时间】:2013-09-13 06:29:57
【问题描述】:

我有数百张图片存储在 SD 卡中。我将这些本地路径放在 sqlite 中,现在我需要使用本地路径显示这些图像我将文件解码为位图,并使用这些 bimap 显示列表视图,这很好但是当滚动该列表视图时,我遇到了这样的 OutOfMemory 问题: 引起:java.lang.OutOfMemoryError:位图大小超出VM预算 在 android.graphics.BitmapFactory.nativeDecodeAsset(本机方法) 我该如何解决这个问题,请提供任何建议。在此先感谢

     public class AllGalleryItemsAdapter extends BaseAdapter {
     int[] adata = null;
     int screenheight,screenwidth;
Context mContext;
private LayoutInflater inflator ;
private int[] children;
int flag;

ArrayList<String> imagepositionPath = new ArrayList<String>();

  ArrayList<ArrayList<String>> imagePath = new ArrayList<ArrayList<String>>();
        ArrayList<ArrayList<Bitmap>> bitmaplist= new ArrayList<ArrayList<Bitmap>>();
ArrayList<String> tagsText = new ArrayList<String>();
ArrayList<Integer> tagig_selected = new ArrayList<Integer>();
private ArrayList<Row> rows; 
//public AllGalleryItemsAdapter(Context context, int[] children) {}
private ViewHolder holder;
private ViewHolder2 holder2;
private Bitmap myBitmapc1,myBitmapc2_1,myBitmapc2_2,myBitmapc3_1,myBitmapc3_2,myBitmapc3_3;
private ViewHolder3 holder3;
File imgFiles1;
    Bitmap b = null; 

public AllGalleryItemsAdapter(Context context,
     ArrayList<String> tag_name,ArrayList<Integer> tag_id, ArrayList<ArrayList<String>> imageList) {
    // TODO Auto-generated constructor stub
           super();

           imagepositionPath = new ArrayList<String>();
         imagePath = new ArrayList<ArrayList<String>>();
             tagsText = new ArrayList<String>();
             bitmaplist= new ArrayList<ArrayList<Bitmap>>();
             tagig_selected = new ArrayList<Integer>();
          imagePath=imageList;
           tagsText=tag_name;
           tagig_selected = tag_id;

           mContext=context;



        @Override
        public int getCount() {
            // TODO Auto-generated method stub

            return tagsText.size();

        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
          public int getItemViewType(int position) {

                return imagePath.get(position).size();
            }

         @Override
            public int getViewTypeCount() {
                return 4;
            }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        View view = null;

        switch (getItemViewType(position)) {

        default:

            if(convertView==null)
            {
                inflator = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                vg = (ViewGroup) inflator.inflate(R.layout.listone, null);
                holder = new ViewHolder();

                holder.listoneimage = (ImageView )vg.findViewById(R.id.singleimage); 
                holder.tagtext = (TextView)vg.findViewById(R.id.tagtext);

                vg.setTag(holder); 
                view=vg;

             }else{
                 holder = (ViewHolder) convertView.getTag();

                         view=convertView;
                    }


                             holder.tagtext.setText("#"+tagsText.get(position));
                        File f = new File(imagePath.get(position).get(0))
                        b= null;
                             b = decodeFile(f);
                        holder.listoneimage.setImageBimap(b);

                     holder.listoneimage.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            int tagid = tagig_selected.get(position);
                            //System.out.println("this is tagidddddddddddddd in adapterrrrrrr "+tagid);
                            Intent in = new Intent(mContext.getApplicationContext(),AllGalleryItemsDetailsViewActivity.class);
                            in.putExtra("tagId", tagid);
                            startActivity(in);
                        }
                    });
                     break;


        case 2:

            if(convertView==null)
            {
                inflator = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                vg = (ViewGroup) inflator.inflate(R.layout.listsecond, null);
                holder2 = new ViewHolder2();

                holder2.listsecondleftimage = (ImageView)vg.findViewById(R.id.singlesecondimage);            
               holder2.listsecondthirdimage = (ImageView)vg.findViewById(R.id.singlesecondthirdimage);
               holder2.tagtext = (TextView)vg.findViewById(R.id.tagtextintwo);


                 vg.setTag(holder2); 
                 view=vg;
            } else {      

                holder2 = (ViewHolder2) convertView.getTag();  
                holder2.listsecondleftimage.setImageBitmap(null);
                 holder2.listsecondthirdimage.setImageBitmap(null);
                view=convertView;
                } 
            holder2.tagtext .setText("#"+tagsText.get(position));

            File f1 = new File(imagePath.get(position).get(0))
                        b= null;
                             b = decodeFile(f1);
            holder2.listsecondleftimage.setImageBitmap(b);
        File f2 = new File(imagePath.get(position).get(1))
                        b= null;
                             b = decodeFile(f2);
            holder2.listsecondthirdimage.setImageBitmap(b);

                holder2.listsecondleftimage.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        int tagid = tagig_selected.get(position);
                        Intent in = new Intent(mContext.getApplicationContext(),AllGalleryItemsDetailsViewActivity.class);
                        in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                        in.putExtra("tagId", tagid);
                        startActivity(in);

                    }
                });
                holder2.listsecondthirdimage.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        int tagid = tagig_selected.get(position);
                        Intent in = new Intent(mContext.getApplicationContext(),AllGalleryItemsDetailsViewActivity.class);
                        in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                        in.putExtra("tagId", tagid);
                        startActivity(in);
                    }
                });
                break;

        case 3:

            if(convertView==null)
            {
                inflator = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                vg = (ViewGroup) inflator.inflate(R.layout.list_secondview,null);
                holder3 = new ViewHolder3();

                holder3.iconsecond = (ImageView)vg.findViewById(R.id.largeinlistseconddynamic);            
               holder3.favsecond = (ImageView)vg.findViewById(R.id.smalimagefirstdynamic);
              holder3.sharesecond = (ImageView)vg.findViewById(R.id.smallimageseconddynamic);
              holder3.tagtext = (TextView)vg.findViewById(R.id.tagtextinthree);


                vg.setTag(holder3); 
                view=vg;
            }else{

                  holder3 = (ViewHolder3) convertView.getTag();

                  view=convertView;
                }

            holder3.tagtext .setText("#"+tagsText.get(position));


            // 
        File f12 = new File(imagePath.get(position).get(0))
                        b= null;
                             b = decodeFile(f12);
            holder3.iconsecond.setImageBitmap(b);
        File f22 = new File(imagePath.get(position).get(1))
                        b= null;
                             b = decodeFile(f22);
             holder3.favsecond.setImageBitmap(b);


            File f23 = new File(imagePath.get(position).get(2))
                        b= null;
                             b = decodeFile(f23);
             holder3.sharesecond.setImageBitmap(b);

            holder3.iconsecond.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        int tagid = tagig_selected.get(position);
                        Intent in = new Intent(mContext.getApplicationContext(),AllGalleryItemsDetailsViewActivity.class);
                        in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                        in.putExtra("tagId", tagid);
                        startActivity(in);
                    }
                });
             holder3.favsecond.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    int tagid = tagig_selected.get(position);
                    Intent in = new Intent(mContext.getApplicationContext(),AllGalleryItemsDetailsViewActivity.class);
                    in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                    in.putExtra("tagId", tagid);
                    startActivity(in);
                }
            });
             holder3.sharesecond.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    int tagid = tagig_selected.get(position);
                    Intent in = new Intent(mContext.getApplicationContext(),AllGalleryItemsDetailsViewActivity.class);
                    in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                    in.putExtra("tagId", tagid);
                    startActivity(in);
                }
            });
             break;


        }

        return view;
    }
}



 class ViewHolder {  
        TextView tagtext,desc,liketext,sharetext,text1,text2,text3,secondtitle,seconddesc,secondshare,secondlike;  
       // ImageView icon,fav,like,share,secondicon,iconsecond,favsecond,sharesecond,listsecondleftimage,listsecondthirdimage;
        ImageView listoneimage;

    }  

 class ViewHolder2 {  
        TextView title,desc,liketext,sharetext,text1,text2,text3,secondtitle,seconddesc,secondshare,secondlike,tagtext;  
       // ImageButton icon,fav,like,share,secondicon,iconsecond,favsecond,sharesecond,listoneimage;
        ImageView listsecondthirdimage;
        ImageView listsecondleftimage;

    }

 class ViewHolder3 {  
        TextView title,desc,liketext,sharetext,text1,text2,text3,secondtitle,seconddesc,secondshare,secondlike,tagtext;  
        //ImageButton icon,fav,like,share,secondicon,listoneimage,listsecondleftimage,listsecondthirdimage;
        ImageView sharesecond;
        ImageView favsecond;
        ImageView iconsecond;

    }  

这是我的解码文件方法

    public Bitmap decodeFile(String filePath) {
    try {
        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, o);
        // The new size we want to scale to
        final int REQUIRED_SIZE = LoginActiviy.screenWidth;;

        // Find the correct scale value. It should be the power of 2.
        int scale = 1;
        while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
            scale *= 2;

        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeFile(filePath, o2);
    } catch (Throwable e) {
        e.printStackTrace();
    }
    return null;

}

【问题讨论】:

  • 你能把你的适配器的代码贴出来吗?
  • 您是否使用图像显示为小缩略图???

标签: android bitmap


【解决方案1】:

试试这行代码:

img.setImageURI(Uri.fromFile(new File(Pathstring)));

这里,Path字符串为本地图片文件路径。分配给图像。

希望对你有帮助!!

【讨论】:

  • 嗨,Armaan 感谢您的回复。我尝试了这个仍然遇到相同的错误,例如 E/dalvikvm-heap(10121): Out of memory on a 7680016-byte allocation。在 android.graphics.BitmapFactory.nativeDecodeAsset(Native Method).
  • 这是一个内存不足异常。您的图像在系统缓存中加载时内存不足。所以使用位图比例制作它。
  • 尝试 biraj 的回答来获取位图以避免内存不足异常。
  • 请提供任何用于缩放位图的代码。我尝试了 Biraj soln,但是当我滚动列表视图时,本机堆大小增加了如何处理本机堆内存。
【解决方案2】:

试试这个函数从路径获取位图

public Bitmap decodeFile(String filePath) {
        try {
            // Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(filePath, o);
            // The new size we want to scale to
            final int REQUIRED_SIZE = 70;

            // Find the correct scale value. It should be the power of 2.
            int scale = 1;
            while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
                scale *= 2;

            // Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            return BitmapFactory.decodeFile(filePath, o2);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return null;

    }

【讨论】:

  • 嗨 Biraj 我用这个仍然出现错误,因为当我滚动列表视图时本机堆大小总是在增长(这里在列表视图中使用不同的行类型)。
  • 你能发布你的列表适配器吗?
  • 我在上面添加了我的代码,请检查一次并给我建议
猜你喜欢
  • 2011-05-10
  • 1970-01-01
  • 2011-11-16
  • 1970-01-01
  • 1970-01-01
  • 2019-01-08
  • 2019-01-13
  • 2016-03-09
  • 1970-01-01
相关资源
最近更新 更多