Question

I am running into an issue where I would like to convert a ResID (Int Value) that I have stored into a SQLite DB into a Drawable. This drawable should be set as the picture associated with its "event object" that is being populated in a listview.

What am I missing? Where should I be looking to convert the resId back into a Drawable to st it?

NOTE: I am trying to do the conversion in EventAdapter Class.

DatabaseHandler

public class DatabaseHandler extends SQLiteOpenHelper {

// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "scheduleManager";

// Contacts table name
private static final String TABLE_EVENTS = "events";


// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_TITLE = "title";
private static final String KEY_TIME = "time";
private static final String KEY_DATE = "date";
private static final String KEY_IMAGE = "image";

public DatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    String CREATE_EVENTS_TABLE = "CREATE TABLE " + TABLE_EVENTS + "("
            + KEY_ID + " INTEGER," + KEY_TITLE + " TEXT,"
            + KEY_TIME + " TEXT," + KEY_DATE + " TEXT," + KEY_IMAGE + " BLOB" + ")";
    db.execSQL(CREATE_EVENTS_TABLE);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_EVENTS);

    // Create tables again
    onCreate(db);

}

/**
 * All CRUD(Create, Read, Update, Delete) Operations
 */


//adding an event (NEEDS TO ADD DRAWABLE)
 public void addEvent(Event event) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_ID, event.get_Id()); //Event ID
        values.put(KEY_TITLE, event.get_title()); // Event Title
        values.put(KEY_TIME, event.get_time()); // Event Time
        values.put(KEY_DATE, event.get_date()); // Event Date
        values.put(KEY_IMAGE, event.get_image()); // Event RESOURCEID

        // Inserting Row
        db.insert(TABLE_EVENTS, null, values);
        db.close(); // Closing database connection
    }

   // Getting single contact
   public Event getEvent(int id) {
       SQLiteDatabase db = this.getReadableDatabase();

       Cursor cursor = db.query(TABLE_EVENTS, new String[] { KEY_ID,
               KEY_TITLE, KEY_TIME, KEY_DATE, KEY_IMAGE }, KEY_ID + "=?",
               new String[] { String.valueOf(id) }, null, null, null, null);
       if (cursor != null)
           cursor.moveToFirst();

       Event event = new Event(Integer.parseInt(cursor.getString(0)),
               cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getBlob(4));
       // return contact
       return event;
   }


    // Getting All Contacts
   public List<Event> getAllContacts() {
      List<Event> eventList = new ArrayList<Event>();
      // Select All Query
      String selectQuery = "SELECT  * FROM " + TABLE_EVENTS;

      SQLiteDatabase db = this.getWritableDatabase();
      Cursor cursor = db.rawQuery(selectQuery, null);

      // looping through all rows and adding to list
      if (cursor.moveToFirst()) {
          do {

              Event event = new Event();
              event.set_Id(Integer.parseInt(cursor.getString(0)));
              event.set_title(cursor.getString(1));
              event.set_time(cursor.getString(2));
              event.set_date(cursor.getString(3));                          
              event.set_image(cursor.getBlob(4));
              eventList.add(event);
          } while (cursor.moveToNext());
      }

      // return contact list
      return eventList;
  }

// Getting event Count
    public int getEventsCount() {
        String countQuery = "SELECT  * FROM " + TABLE_EVENTS;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        cursor.close();

        // return count
        return cursor.getCount();
    }

 // Updating single contact
    public int updateEvent(Event event) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_TITLE, event.get_title());
        values.put(KEY_TIME, event.get_time());
        values.put(KEY_DATE, event.get_date());

        // updating row
        return db.update(TABLE_EVENTS, values, KEY_ID + " = ?",
                new String[] { String.valueOf(event.get_Id()) });
    }

    // Deleting single contact
    public void deleteEvent(Event event) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_EVENTS, KEY_ID + " = ?",
                new String[] { String.valueOf(event.get_Id()) });
        db.close();
    }

 }

EventAdapter

public class EventAdapter extends ArrayAdapter<Event> {

// View lookup cache
private static class ViewHolder {
    //adding drawable to imageview
    ImageView img;
    TextView title;
    TextView time;
    TextView date;
}

public EventAdapter(Context context, List<Event> objects) {
    super(context, R.layout.date_detail);
    // TODO Auto-generated constructor stub
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Get the data item for this position
    Event event = getItem(position);
    // Check if an existing view is being reused, otherwise inflate the view
    ViewHolder viewHolder;
    if (convertView == null) {
        viewHolder = new ViewHolder();
        LayoutInflater inflater = LayoutInflater.from(getContext());
        convertView = inflater.inflate(R.layout.date_detail, null);
        viewHolder.title = (TextView) convertView
                .findViewById(R.id.textViewTitle);
        viewHolder.time = (TextView) convertView
                .findViewById(R.id.textViewTime);
        viewHolder.date = (TextView) convertView
                .findViewById(R.id.textViewDate);

        //adding drawable to imageview
        viewHolder.img = (ImageView) convertView
                .findViewById(R.id.imageView1);


        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    // Populate the data into the template view using the data object
    viewHolder.title.setText(event._title);
    viewHolder.time.setText(event._time);
    viewHolder.date.setText(event._date);

    //convert from byte array to bitmap               
    Bitmap bitmap = convertByteArrayToBitmap(event._image);
    Drawable drawable = new BitmapDrawable(bitmap);
    // CONVERT BITMAP TO DRAWABLE
    viewHolder.img.setImageDrawable(drawable);
    // Return the completed view to render on screen
    return convertView;
}

public static Bitmap convertByteArrayToBitmap(
        byte[] byteArrayToBeCOnvertedIntoBitMap)
    {
        Bitmap bitmap = BitmapFactory.decodeByteArray(
            byteArrayToBeCOnvertedIntoBitMap, 0,
            byteArrayToBeCOnvertedIntoBitMap.length);
        return bitmap;
    }
}

MainActivity

public class MainActivity extends FragmentActivity implements OnClickListener {

ListView listView;
int lastIndex = -1;
ArrayList<Event> lstEvents = new ArrayList<Event>();

// detail view
TextView tvTitle, tvTime, tvDate;
ImageView ivPic;
View vw_master;

boolean _isBack = true;

ImageButton add;

String title;
String date;
String time;
int resId;

static final int PICK_CONTACT_REQUEST = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    // // get detail controls
    tvTitle = (TextView) findViewById(R.id.textViewTitle);
    tvDate = (TextView) findViewById(R.id.textViewDate);
    tvTime = (TextView) findViewById(R.id.textViewTime);
    ivPic = (ImageView) findViewById(R.id.imageView1);

    add = (ImageButton) findViewById(R.id.add);
    add.setOnClickListener(this);

    // ///////////////////////////////LISTVIEW////////////////////////////////////////
    // Create the adapter to convert the array to views
    EventAdapter adapter = new EventAdapter(this, lstEvents);

    // attach adapter to a list view
    listView = (ListView) findViewById(R.id.listViewFragment);
    listView.setAdapter(adapter);
    // ///////////////////////////////LISTVIEW////////////////////////////////////////

    // /////////////////////////////DATABASE/////////////////////////////////////////////
    DatabaseHandler db = new DatabaseHandler(this);
    // /////////////////////////////DATABASE/////////////////////////////////////////////

    List<Event> events = db.getAllContacts();

    adapter.addAll(events);
    adapter.notifyDataSetChanged();

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.add:
        Intent intent = new Intent(this, CreateActivity.class);
        startActivityForResult(intent, 100);
        break;
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    // /////////////////////////////DATABASE/////////////////////////////////////////////
    DatabaseHandler db = new DatabaseHandler(this);
    // /////////////////////////////DATABASE/////////////////////////////////////////////

    // Create the adapter to convert the array to views
    EventAdapter adapter = new EventAdapter(this, lstEvents);

    // attach adapter to a list view
    listView = (ListView) findViewById(R.id.listViewFragment);
    listView.setAdapter(adapter);

    if (requestCode == 100) {
        if (resultCode == RESULT_OK) {
            Bundle b = data.getExtras();
            title = b.getString("TITLE");
            time = b.getString("TIME");
            date = b.getString("DATE");

            Bitmap bitmap = b.getParcelable("BITMAP");


            /////CONVERTING A BITMAP TO A BYTE[]
            byte[] image = null;
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
            image = bos.toByteArray();
            /////////




            // /////////////////////////////DATABASE/////////////////////////////////////////////
            /**
             * CRUD OPERATIONS
             */

            Log.e("Insert: ", "Inserting ..");
            db.addEvent(new Event(0, title, time, date, image));

            // Reading all contacts
            Log.e("Reading: ", "Reading all contacts..");
            // List<Event> events = db.getAllContacts();
            List<Event> events = db.getAllContacts();
            adapter.addAll(events);
            adapter.notifyDataSetChanged();

            // logging all events

            for (Event ev : events) {
                String log = "Id: " + ev.get_Id() + " ,Title: "
                        + ev.get_title() + " ,Date: " + ev.get_date()
                        + " ,RESOURCEID: " + ev.get_image();
                // Writing Contacts to log
                Log.e("Name: ", log);

            }

            // /////////////////////////////DATABASE/////////////////////////////////////////////
        }

    }
}
Was it helpful?

Solution

EDIT: Take a look at this answer. Quoting its poster (@Tom), I think there's no way to fetch an id from a Drawable object. The instance returned by getDrawable does not contain such information. Better yet, you can use the fact that getDrawable can throw a NotFoundException if the id is somehow wrong. Check the docs for details. Link to Docs

So, my recommendation is to change the way that you are storing the image in your SQLite Database. The best way that I have found is using the BLOB type.

First let's change that implementation:

In your DatabaseHandler:

Change,

String CREATE_EVENTS_TABLE = "CREATE TABLE " + TABLE_EVENTS + "("
        + KEY_ID + " INTEGER," + KEY_TITLE + " TEXT,"
        + KEY_TIME + " TEXT," + KEY_DATE + " TEXT," + KEY_RESID + " INTEGER" + ")";

to this:

String CREATE_EVENTS_TABLE = "CREATE TABLE " + TABLE_EVENTS + "("
        + KEY_ID + " INTEGER," + KEY_TITLE + " TEXT,"
        + KEY_TIME + " TEXT," + KEY_DATE + " TEXT," + KEY_RESID + " BLOB" + ")";

Also: Make sure that the object Event is implemented to use byte[] (getters and setters) instead of that int. For instance, event.get_resId() should return a byte[] and, so on.

Then, to convert a Bitmap to a byte[] you would have to implement this code:

byte[] image = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
image = bos.toByteArray();

Now, you can add the image to your database.

In the same way, to convert the stored byte[] to a Bitmap, you would have to do this:

Bitmap bitmap = BitmapFactory.decodeByteArray(image, 0, image.length);

I hope this helps. Feel free to ask if you have any questions.

OTHER TIPS

You should not store ResourceId values in a database. They can change each time you add new module or library which has own generated files.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top