关于在sqlite db中存储图像的java.lang.OutOfMemoryError

Jeo*_*ebs 14 java sqlite android bitmap out-of-memory

我想将图像存储在我的数据库中.另外,我想检查图像和标题是否已存在于数据库中.如果是这样,它将不会将它们添加到数据库中.这是我的班级.

景点

public class Attractions extends ListActivity {
DataBaseHandler db = new DataBaseHandler(this);
ArrayList<Contact> imageArry = new ArrayList<Contact>();
List<Contact> contacts;
ContactImageAdapter adapter;
int ctr, loaded; 
int [] landmarkImages={R.drawable.oblation,R.drawable.eastwood,R.drawable.ecopark,R.drawable.circle};
String []landmarkDetails = { "Oblation", "Eastwood", "Ecopark", "QC Circle"};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_attractions);
    ctr  = db.checkContact(landmarkDetails[loaded]);



    // get image from drawable

    /**
     * CRUD Operations
     * */
    // Inserting Contacts
    Log.d("Insert: ", "Inserting ..");


    for(loaded=0; loaded <landmarkDetails.length;loaded++){

        Bitmap image = BitmapFactory.decodeResource(getResources(),
                landmarkImages[loaded]);


        // convert bitmap to byte
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        byte imageInByte[] = stream.toByteArray();
        Log.d("Going to load images", "Image "+ loaded);

        Log.d("Goind to load objects", "loading");

        if(ctr == 0){
            Log.d("Nothing Loaded", "Loading Now");
            db.addContact(new Contact(landmarkDetails[loaded], imageInByte));}
            Log.d(landmarkDetails[loaded], "Loaded!");
            image.recycle();
    }
    loadFromDb();


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.attractions, menu);


    return true;
}

public void loadFromDb(){
    // Reading all contacts from database
            contacts = db.getAllContacts();
            for (Contact cn : contacts) {
                String log = "ID:" + cn.getID() + " Name: " + cn.getName()
                        + " ,Image: " + cn.getImage();

                // Writing Contacts to log
                Log.d("Result: ", log);
                //add contacts data in arrayList
                imageArry.add(cn);

            }
            adapter = new ContactImageAdapter(this, R.layout.screen_list,
                    imageArry);
            ListView dataList = (ListView) findViewById(android.R.id.list);
            dataList.setAdapter(adapter);

}

public void onPause(){
    super.onPause();
}

public void onResume(){
    super.onResume();

}


}
Run Code Online (Sandbox Code Playgroud)

它在模拟器上工作正常,但我尝试在我的S4上测试,然后在3次尝试进入这个类之后,它被迫停止.我用usb调试尝试了它,logcat显示了java.lang.outofmemoryerror.logcat在我的contactimageadapter中指出了错误.

ContactImageAdapter

public class ContactImageAdapter extends ArrayAdapter<Contact>{
 Context context;
    int layoutResourceId;   
   // BcardImage data[] = null;
    ArrayList<Contact> data=new ArrayList<Contact>();
    public ContactImageAdapter(Context context, int layoutResourceId, ArrayList<Contact> data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ImageHolder holder = null;

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new ImageHolder();
            holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
            holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
            row.setTag(holder);
        }
        else
        {
            holder = (ImageHolder)row.getTag();
        }

        Contact picture = data.get(position);
        holder.txtTitle.setText(picture._name);
        //convert byte to bitmap take from contact class

        byte[] outImage=picture._image;
        ByteArrayInputStream imageStream = new ByteArrayInputStream(outImage);
        Bitmap theImage = BitmapFactory.decodeStream(imageStream);
        holder.imgIcon.setImageBitmap(theImage);
       return row;

    }

    static class ImageHolder
    {
        ImageView imgIcon;
        TextView txtTitle;
    }
}
Run Code Online (Sandbox Code Playgroud)

并指出这一行 Bitmap theImage = BitmapFactory.decodeStream(imageStream);

我对管理图像和存储图像知之甚少(几乎没有).我也启用android:largeHeap但仍然强制关闭多次尝试.我希望有人可以帮我解决这个问题,或者至少向我展示一种将文本和图像存储到sqlite db的不同方式.非常感谢!

Vad*_*dim 10

你有多个地方整个图像(假设它很大)保留在内存中:

  1. 联系对象有它.所有加载的图像都在imageArry中,它是实例级变量.

    public class Attractions extends ListActivity {
        DataBaseHandler db = new DataBaseHandler(this);
        ArrayList<Contact> imageArry = new ArrayList<Contact>();
    
    Run Code Online (Sandbox Code Playgroud)
  2. ContactImageAdapter.getView方法中,您在持有者对象中创建另一个图像副本作为BMP并将其传递出方法.所以,在某些时候你没有足够的内存来保留所有内容.此外,我确定decodeStream需要更多的内存来执行.

毕竟,创建的每个新持有者都将无法预测getViewGC.通常对于这种情况,当对象在某些方法中创建为new时,然后传递回调用方法,该对象将仅由Full GC收集.

因此,正如"软件Sainath"所说,不要将图像存储在数据库中......也不要将它们保存在内存中.

PS然后向视图提供外部图像文件的链接.这也将节省加载视图的时间.图像将在缓存中,如果用户至少获得一次,则不会再次通过网络.

我想那里的图像不会经常改变它们自己.联系人的另一个图像将是另一个文件......


小智 6

我刚才写了一个类似问题的答案,这里是你可以检查的链接.问题在于将图像保存到数据库中的方法,您不应该这样做.而是将图像作为文件写在手机存储器上并进一步使用.