如何在SQLite(Android)中存储可绘制/位图
我想通过CreateActivity中的Bundle传递一个Drawable的Int值。根据这个Int值(资源ID),我想使用它创建一个存储在SQLLITE数据库中的事件对象。当程序生成listview时,它应该能够使用此Int值将其关联的Drawable分配给listview的一部分 **我应该怎么做才能在SQLite DB中存储一个Drawable,或者至少在DB中存储INT值** 主要活动如何在SQLite(Android)中存储可绘制/位图,android,sqlite,bitmap,drawable,android-drawable,Android,Sqlite,Bitmap,Drawable,Android Drawable,我想通过CreateActivity中的Bundle传递一个Drawable的Int值。根据这个Int值(资源ID),我想使用它创建一个存储在SQLLITE数据库中的事件对象。当程序生成listview时,它应该能够使用此Int值将其关联的Drawable分配给listview的一部分 **我应该怎么做才能在SQLite DB中存储一个Drawable,或者至少在DB中存储INT值** 主要活动 public class MainActivity extends FragmentActivity
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");
// retrieving bitmap from CreateActivity
Bitmap bitmap = (Bitmap) b.getParcelable("DRAWABLE");
// converting from bitmap to drawable
Drawable drawable = new BitmapDrawable(getResources(), bitmap);
// Event newEvent = new Event();
// newEvent.set_date(date);
// newEvent.set_title(title);
// newEvent.set_time(time);
// set drawable
// newEvent.set_drawable(drawable);
// lstEvents.add(newEvent);
// adapter.addAll(lstEvents);
// adapter.notifyDataSetChanged();
///////////////////////////////DATABASE/////////////////////////////////////////////
/**
* CRUD OPERATIONS
*/
Log.e("Insert: ", "Inserting ..");
db.addEvent(new Event(0, title, time, date, drawable));
// 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();
// Writing Contacts to log
Log.e("Name: ", log);
}
///////////////////////////////DATABASE/////////////////////////////////////////////
}
}
}
}
public class CreateActivity extends Activity implements OnClickListener {
EditText etTitle;
Button btDate;
Button btTime;
Button btPic;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//onclicklistener
findViewById(R.id.btn_confirm).setOnClickListener(this);
findViewById(R.id.btn_back).setOnClickListener(this);
etTitle = (EditText) findViewById(R.id.editTextTitle);
btDate = (Button) findViewById(R.id.btn_date);
btTime = (Button) findViewById(R.id.btn_time);
btPic = (Button) findViewById(R.id.btn_picture);
}
// Will be called via the onClick attribute
// of the buttons in main.xml
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_confirm:
String title = etTitle.getText().toString();
String time = btTime.getText().toString();
String date = btDate.getText().toString();
Drawable drawable = btPic.getCompoundDrawables()[1];
Log.e("DRAWABLE", "DrawableId" + drawable);
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
Log.e("LOG", title);
Log.e("LOG", time);
Log.e("LOG", date);
Bundle newBundle = new Bundle();
newBundle.putString("TITLE", title);
newBundle.putString("TIME", time);
newBundle.putString("DATE", date);
//Trying to pass a drawable from one activity to another
newBundle.putParcelable("DRAWABLE", bitmap);
Intent intent = new Intent(this, MainActivity.class);
intent.putExtras(newBundle);
setResult(RESULT_OK, intent);
finish();
break;
case R.id.btn_back:
finish();
break;
}
}
public void showTimePickerDialog(View v) {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getFragmentManager(), "timePicker");
}
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");
}
public void showPicturePickerDialog(View v) {
DialogFragment newFragment = new PicturePickerFragment();
newFragment.show(getFragmentManager(), "picturePicker");
}
}
不建议在数据库中存储图像。必须将图像存储在内部或外部内存中,并将图像的引用存储在数据库中
// GET INT VALUE FROM RESOURCE
int home = R.drawable.home;
// SET IMAGE RESOURCE
img.setImageResource(home);
如果要将图像放在项目的内部内存中,请将图像放在资产文件夹中,并将其名称保存在sqlite中,然后使用以下代码显示图像:
String imageUrl = imgArraList.get(position).getUrl();
/*imageUrl variable will contain image name that should be available in assets folder.*/
Bitmap bitmap = BitmapFactory.decodeStream(getAssets().open(imageUrl));
touch.setImageBitmap(bitmap, null, 1.0f, 2.0f);
资源是否已存储在内存中?它们位于drawable文件夹内,因此我将如何获取对图像的引用?如果图像位于
/drawable
文件夹内,则R.drawable.image\u name
实际上是一个整数。将此整数存储到DB中,下次重新使用。如何将整数转换回可绘制的?为此,我将存储R.drawable.home
,还是只存储home
?另外,这对字符串资源也有效吗?@VishnuHaridas这个整数一致吗?当重新编译应用程序或添加其他图像时,它不会改变吗?
// GET INT VALUE FROM RESOURCE
int home = R.drawable.home;
// SET IMAGE RESOURCE
img.setImageResource(home);
String imageUrl = imgArraList.get(position).getUrl();
/*imageUrl variable will contain image name that should be available in assets folder.*/
Bitmap bitmap = BitmapFactory.decodeStream(getAssets().open(imageUrl));
touch.setImageBitmap(bitmap, null, 1.0f, 2.0f);