我想将我的imageview上的图像保存到数据库中,但我无法将imageview上的图像保存到数据库中。
MainActivity.java
protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case REQUEST_CODE:
if (data != null) {
int id = dbHandler.getEntriesCount();
long time = System.currentTimeMillis();
SimpleDateFormat dayTime = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
String date = dayTime.format(new Date(time));
String title = data.getStringExtra("Title");
String passage = data.getStringExtra("Passage");
Uri imageUri = Uri.parse(data.getStringExtra("imageUri"));
Entry entry = new Entry(id, title, passage, imageUri, date);
dbHandler.createEntry(entry);
Entry Entry = dbHandler.getEntry(id);
entryList.add(Entry);
EntryListadapter.notifyDataSetChanged();
Acitivity_Write.Java
SaveBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent data = new Intent();
data.putExtra("Title", TitleTxt.getText().toString());
data.putExtra("Passage", WriteTxt.getText().toString());
data.putExtra("imageUri", Uri.parse(EntryImgImageView.toString()));
setResult(RESULT_OK, data);
finish();
Toast.makeText(getApplicationContext(), "Entry Added", Toast.LENGTH_SHORT).show();
Entry.Java
public class Entry {
public String _title, _passage;
public Uri _imageUri;
public String _date;
public int _id;
public Entry (int id, String title, String passage, Uri imageUri, String date) {
_id = id;
_title = title;
_passage = passage;
_imageUri = imageUri;
_date = date;
}
public int getId() { return _id; }
public String getTitle() { return _title; }
public String getPassage() { return _passage; }
public Uri getImageUri() { return _imageUri; }
public String getDate() { return _date; }
}
DatabaseHandler.java
public class DatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "entriesManager",
TABLE_ENTRIES = "entries",
KEY_ID = "id",
KEY_TITLE = "title",
KEY_PASSAGE = "passage",
KEY_IMAGEURI = "imageUri",
KEY_DATE="date";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_ENTRIES + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_TITLE + " TEXT," + KEY_PASSAGE + " TEXT," + KEY_IMAGEURI + " URI," + KEY_DATE + " TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_ENTRIES);
onCreate(db);
}
public void createEntry(Entry entry) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_ID,entry.getId());
values.put(KEY_TITLE, entry.getTitle());
values.put(KEY_PASSAGE, entry.getPassage());
values.put(KEY_IMAGEURI, entry.getImageUri().toString());
values.put(KEY_DATE, entry.getDate());
db.insert(TABLE_ENTRIES, null, values);
db.close();
}
错误
12-16 03:11:09.210 3217-3217 / unipersonalinc.mypersonalediary E / AndroidRuntime:致命的例外:主要 12-16 03:11:09.210 3217-3217 / unipersonalinc.mypersonalediary E / AndroidRuntime:流程:unipersonalinc.mypersonalediary,PID:3217 12-16 03:11:09.210 3217-3217 / unipersonalinc.mypersonalediary E / AndroidRuntime:java.lang.RuntimeException:交付失败 结果ResultInfo {who = null,request = 100,result = -1,data = Intent {(has 额外的)}}到活动 {unipersonalinc.mypersonalediary / unipersonalinc.mypersonalediary.MainActivity}: java.lang.NullPointerException:uriString 12-16 03:11:09.210 3217-3217 / unipersonalinc.mypersonalediary E / AndroidRuntime:at android.app.ActivityThread.deliverResults(ActivityThread.java:3699) 12-16 03:11:09.210 3217-3217 / unipersonalinc.mypersonalediary E / AndroidRuntime:at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742) 12-16 03:11:09.210 3217-3217 / unipersonalinc.mypersonalediary E / AndroidRuntime:at android.app.ActivityThread.-WRAP16(ActivityThread.java) 12-16 03:11:09.210 3217-3217 / unipersonalinc.mypersonalediary E / AndroidRuntime:at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1393) 12-16 03:11:09.210 3217-3217 / unipersonalinc.mypersonalediary E / AndroidRuntime:at android.os.Handler.dispatchMessage(Handler.java:102) 12-16 03:11:09.210 3217-3217 / unipersonalinc.mypersonalediary E / AndroidRuntime:at android.os.Looper.loop(Looper.java:148) 12-16 03:11:09.210 3217-3217 / unipersonalinc.mypersonalediary E / AndroidRuntime:at android.app.ActivityThread.main(ActivityThread.java:5417) 12-16 03:11:09.210 3217-3217 / unipersonalinc.mypersonalediary E / AndroidRuntime:at java.lang.reflect.Method.invoke(Native 方法) 12-16 03:11:09.210 3217-3217 / unipersonalinc.mypersonalediary E / AndroidRuntime:at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) 12-16 03:11:09.210 3217-3217 / unipersonalinc.mypersonalediary E / AndroidRuntime:at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 12-16 03:11:09.210 3217-3217 / unipersonalinc.mypersonalediary E / AndroidRuntime:引起:java.lang.NullPointerException: uriString中 12-16 03:11:09.210 3217-3217 / unipersonalinc.mypersonalediary E / AndroidRuntime:在android.net.Uri $ StringUri。
12-16 03:11:09.210 3217-3217 / unipersonalinc.mypersonalediary E / AndroidRuntime:at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:0)
保存图像的正确方法不在数据库中。将图像的名称保存在数据库中,并在app目录中写入具有相同名称的Image。
检索图像使用名称读取图像并将其设置为ImageView
。