从文件路径向图像显示图像

时间:2013-09-13 06:29:58

标签: android bitmap

我有一些存储在SD卡中的图像。我把这些本地路径放在sqlite中我现在需要使用本地路径显示这些图像我解码文件到位图我使用那些bimaps显示listview这很好但是当滚动那个listview我有这样的OutOfMemmory问题:      引起: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;

}

2 个答案:

答案 0 :(得分:0)

试试这行代码:

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

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

希望它能帮助!!

答案 1 :(得分:0)

尝试此功能从路径

获取位图
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;

    }