ANDROID - 将ImageView中的图像保存到数据库

时间:2016-01-18 13:58:31

标签: android database image sqlite

我做了一个应用程序,到目前为止我将捕获的图像存储在数据库中。然后我将它们分类并在每个类别的不同GridView中显示它们。在我从每个gridView中选择一个图像后,我设法在不同的ImageViews中显示它们。现在,我试图将这些选择的图像保存在数据库中,然后在ListView中显示它们。问题是列表中没有任何内容。我想我的错误在于代码中我正在拍摄图像来保存它们。

任何帮助?

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view);

    handler2 = new DatabaseHandler(getApplicationContext());

    Intent i = getIntent();

    ImageView im1 = (ImageView) findViewById(R.id.im1);
    im1.setImageBitmap(BitmapFactory.decodeFile(i.getStringExtra("image1")));

    ImageView im2 = (ImageView) findViewById(R.id.im2);
    im2.setImageBitmap(BitmapFactory.decodeFile(i.getStringExtra("image2")));

    ImageView im3 = (ImageView) findViewById(R.id.im3);
    im3.setImageBitmap(BitmapFactory.decodeFile(i.getStringExtra("image3")));

    ImageView im4 = (ImageView) findViewById(R.id.im4);
    im4.setImageBitmap(BitmapFactory.decodeFile(i.getStringExtra("image4")));

    ImageView im5 = (ImageView) findViewById(R.id.im5);
    im5.setImageBitmap(BitmapFactory.decodeFile(i.getStringExtra("image5")));

    ImageView im6 = (ImageView) findViewById(R.id.im6);
    im6.setImageBitmap(BitmapFactory.decodeFile(i.getStringExtra("image6")));

    ImageButton btn_save_outfit = (ImageButton)findViewById(R.id.btn_combine);
    btn_save_outfit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Outfits outfits = new Outfits();
            outfits.setImage1(i1);
            outfits.setImage2(i2);
            outfits.setImage3(i3);
            outfits.setImage4(i4);
            outfits.setImage5(i5);
            outfits.setImage6(i6);

            Boolean added = handler2.addOutfit(outfits);
            if(added){
                Toast.makeText(getApplicationContext() , "Outfit added." , Toast.LENGTH_LONG).show();

                String log = "Id: "+ outfits.getID()+" ,Image1: " + outfits.getImage1() + " ,Image2: " + outfits.getImage2()

                        + ",Image3:" + outfits.getImage3() + ",Image4:" + outfits.getImage5() + ",Image6:" +outfits.getImage6();

                Log.d("Image1: ", log);


            }else{
                Toast.makeText(getApplicationContext(), "Outfit not added. Please try again", Toast.LENGTH_LONG).show();
            }


        }});
}



    }

Outfits.class

public class Outfits {

private int _id;
private String _image1;
private String _image2;
private String _image3;
private String _image4;
private String _image5;
private String _image6;

public Outfits(){
}


public Outfits(String image1, String image2, String image3, String image4, String image5 ,String image6){

    this._image1 = image1;
    this._image2 = image2;
    this._image3 = image3;
    this._image4 = image4;
    this._image5 = image5;
    this._image6 = image6;
}

// Id
public int getID(){
    return _id;
}

public void setID(int id){
    this._id = id;
}


// Image1
public String getImage1(){
    return this._image1;
}

public void setImage1(String image1){
    this._image1 = image1;
}


// Image2
public String getImage2(){
    return this._image2;
}

public void setImage2(String image2){
    this._image2 = image2;
}


// Image3
public String getImage3(){
    return this._image3;
}

public void setImage3(String image3){this._image3 = image3;}

// Image4
public String getImage4(){
    return this._image4;
}

public void setImage4(String image4){this._image4 = image4;
}

// Image5
public String getImage5(){
    return this._image5;
}

public void setImage5(String image5){
    this._image5 = image5;
}

// Image6
public String getImage6(){
    return this._image6;
}

public void setImage6(String image6){this._image6 = image6;
}

}

Adapter.class

public class OutfitsAdapter  extends BaseAdapter {

private List<Outfits> items;
private Context context;
private LayoutInflater inflater;

public OutfitsAdapter(Context _context, List<Outfits> _items){

    inflater = LayoutInflater.from(_context);
    this.items = _items;
    this.context = _context;

}

@Override
public int getCount() {
    return items.size();
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Outfits outfits = items.get(position);

    View view = convertView;

    if (view == null) {

        view = inflater.inflate(R.layout.outfit_item, null);


        ImageView i1 = (ImageView) view.findViewById(R.id.image1);
        ImageView i2 = (ImageView) view.findViewById(R.id.image2);
        ImageView i3 = (ImageView) view.findViewById(R.id.image3);
        ImageView i4 = (ImageView) view.findViewById(R.id.image4);
        ImageView i5 = (ImageView) view.findViewById(R.id.image5);
        ImageView i6 = (ImageView) view.findViewById(R.id.image6);

        i1.setImageBitmap(BitmapFactory.decodeFile(outfits.getImage1()));
        i2.setImageBitmap(BitmapFactory.decodeFile(outfits.getImage2()));
        i3.setImageBitmap(BitmapFactory.decodeFile(outfits.getImage3()));
        i4.setImageBitmap(BitmapFactory.decodeFile(outfits.getImage4()));
        i5.setImageBitmap(BitmapFactory.decodeFile(outfits.getImage5()));
        i6.setImageBitmap(BitmapFactory.decodeFile(outfits.getImage6()));
    }
    return view;

}

}

Database.class

public List<Outfits> readAllOutfits(){

    SQLiteDatabase db = this.getWritableDatabase();

    List<Outfits> outfits = new ArrayList<Outfits>();

    Cursor cursor = db.query(SQLITE_TABLE_2, columns_2, null, null, null, null, null);

    cursor.moveToFirst();

    while (!cursor.isAfterLast()) {
        Outfits out = new Outfits();
        out.setID(Integer.parseInt(cursor.getString(0)));
        out.setImage1(cursor.getString(1));
        out.setImage2(cursor.getString(2));
        out.setImage3(cursor.getString(3));
        out.setImage4(cursor.getString(4));
        out.setImage5(cursor.getString(5));
        out.setImage6(cursor.getString(6));

        cursor.moveToNext();
    }

    cursor.close();
    return outfits;
}

带有列表的班级

public class CurrentOutfits extends Activity {

private List<Outfits> outfits;

private DatabaseHandler handler2;

ListView lv_outfits;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.current_outfits);

    handler2 = new DatabaseHandler(getApplicationContext());

    lv_outfits = (ListView) findViewById(R.id.outfits_list);

    loadOutfits();
}

private void loadOutfits() {

    outfits = handler2.readAllOutfits();

    OutfitsAdapter adapter = new OutfitsAdapter(this, outfits);

    lv_outfits.setAdapter(adapter);

    for (Outfits o : outfits) {
        String record = "ID=" + o.getID() + " | Category=" + o.getImage1() + " | " + o.getImage2();
        Log.d("Record", record);
    }

}

}

1 个答案:

答案 0 :(得分:1)

问题出在Database.class中,你错过了一行检查

public List<Outfits> readAllOutfits(){

SQLiteDatabase db = this.getWritableDatabase();

List<Outfits> outfits = new ArrayList<Outfits>();

Cursor cursor = db.query(SQLITE_TABLE_2, columns_2, null, null, null, null, null);

cursor.moveToFirst();

while (!cursor.isAfterLast()) {
    Outfits out = new Outfits();
    out.setID(Integer.parseInt(cursor.getString(0)));
    out.setImage1(cursor.getString(1));
    out.setImage2(cursor.getString(2));
    out.setImage3(cursor.getString(3));
    out.setImage4(cursor.getString(4));
    out.setImage5(cursor.getString(5));
    out.setImage6(cursor.getString(6));

    outfits.add(out);//this is the line you missed

    cursor.moveToNext();
}

cursor.close();
return outfits;
}

希望它有效