我有现有的ListView,它填充了SQLite数据库中的一些信息。我想从SQLite数据库中存储和检索图像,然后在ListView中显示它。我已经为此构建了Databasehandler和Adapter。但是,问题是当我运行我的项目时它会出现未知错误(代码0):Native无法创建新的byte []。
-Logcat
11-10 00:32:24.797: E/AndroidRuntime(2295): FATAL EXCEPTION: main
11-10 00:32:24.797: E/AndroidRuntime(2295): android.database.sqlite.SQLiteException: unknown error (code 0): Native could not create new byte[]
11-10 00:32:24.797: E/AndroidRuntime(2295): at android.database.CursorWindow.nativeGetBlob(Native Method)
11-10 00:32:24.797: E/AndroidRuntime(2295): at android.database.CursorWindow.getBlob(CursorWindow.java:399)
11-10 00:32:24.797: E/AndroidRuntime(2295): at android.database.AbstractWindowedCursor.getBlob(AbstractWindowedCursor.java:45)
11-10 00:32:24.797: E/AndroidRuntime(2295): at kre.db.DatabaseHandler.getAllCard(DatabaseHandler.java:275)
11-10 00:32:24.797: E/AndroidRuntime(2295): at com.example.kre.MyWallet.onActivityCreated(MyWallet.java:58)
11-10 00:32:24.797: E/AndroidRuntime(2295): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:848)
11-10 00:32:24.797: E/AndroidRuntime(2295): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
11-10 00:32:24.797: E/AndroidRuntime(2295): at android.app.BackStackRecord.run(BackStackRecord.java:635)
11-10 00:32:24.797: E/AndroidRuntime(2295): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
11-10 00:32:24.797: E/AndroidRuntime(2295): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
11-10 00:32:24.797: E/AndroidRuntime(2295): at android.os.Handler.handleCallback(Handler.java:615)
11-10 00:32:24.797: E/AndroidRuntime(2295): at android.os.Handler.dispatchMessage(Handler.java:92)
11-10 00:32:24.797: E/AndroidRuntime(2295): at android.os.Looper.loop(Looper.java:137)
11-10 00:32:24.797: E/AndroidRuntime(2295): at android.app.ActivityThread.main(ActivityThread.java:4745)
11-10 00:32:24.797: E/AndroidRuntime(2295): at java.lang.reflect.Method.invokeNative(Native Method)
11-10 00:32:24.797: E/AndroidRuntime(2295): at java.lang.reflect.Method.invoke(Method.java:511)
11-10 00:32:24.797: E/AndroidRuntime(2295): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-10 00:32:24.797: E/AndroidRuntime(2295): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-10 00:32:24.797: E/AndroidRuntime(2295): at dalvik.system.NativeStart.main(Native Method)
-MyWallet
package com.example.kre;
import kre.adapter.ListAdapterMyWallet;
import kre.db.DatabaseHandler;
import kre.model.Card;
import kre.model.Category;
import com.example.login.R;
import android.os.Bundle;
import android.app.Fragment;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;
public class MyWallet extends Fragment {
public MyWallet(){}
ArrayList<Card> cardList = new ArrayList<Card>();
List<Category> listCategory;
ListView listView;
ListAdapterMyWallet adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.mywallet, container, false);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
listView = (ListView) getView().findViewById(R.id.list);
DatabaseHandler db = new DatabaseHandler(getActivity());
/*listCategory = db.getAllCategory();
if(listCategory.size()==0)
{
db.addCategory(new Category("Food"));
db.addCategory(new Category("Shopping"));
db.addCategory(new Category("Health"));
listCategory = db.getAllCategory();
}*/
List<Card> cards = db.getAllCard();
if(cards.size() == 0)
{
Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte imageInByte[] = stream.toByteArray();
db.addCard(new Card(imageInByte, "Pizza Hut", 0));
}
for(Card cd : cards){
String log = "ID: " + cd.getId() + "Image: " + cd.getCardImg() + "Name: " + cd.getCardName()
+ "Card Type Id: " + cd.getCardTypeId();
Log.d("Result: ", log);
cardList.add(cd);
}
adapter = new ListAdapterMyWallet(getActivity(), R.layout.list_row_mywallet, cardList);
listView.setAdapter(adapter);
}
}
-ListAdapterMyWallet
package kre.adapter;
import kre.model.Card;
import com.example.login.R;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.io.ByteArrayInputStream;
import java.util.ArrayList;
public class ListAdapterMyWallet extends ArrayAdapter<Card> {
Context context;
int layoutResourceId;
ArrayList<Card> cardItems = new ArrayList<Card>();
public ListAdapterMyWallet(Context context, int layoutResourceId ,ArrayList<Card> cardItems) {
super(context, layoutResourceId, cardItems);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.cardItems = cardItems;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
CardHolder holder = null;
if(row==null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new CardHolder();
holder.txtCardName = (TextView)row.findViewById(R.id.card_name);
holder.imgIcon = (ImageView)row.findViewById(R.id.card_img);
row.setTag(holder);
}else
{
holder = (CardHolder)row.getTag();
}
Card card = cardItems.get(position);
holder.txtCardName.setText(card.getCardName());
//convert byte to bitmap take from contact class
byte[] outImage=card.getCardImg();
ByteArrayInputStream imageStream = new ByteArrayInputStream(outImage);
Bitmap theImage = BitmapFactory.decodeStream(imageStream);
holder.imgIcon.setImageBitmap(theImage);
return row;
}
static class CardHolder
{
ImageView imgIcon;
TextView txtCardName;
}
}
-DatabaseHandler
package kre.db;
import java.util.ArrayList;
import java.util.List;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import kre.model.Profile;
import kre.model.Card;
import kre.model.Category;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
//Database name
private static final String DATABASE_NAME = "KRE";
//Table names
private static final String TABLE_PROFILE = "profile";
private static final String TABLE_CATEGORY = "category";
private static final String TABLE_CARD = "card";
//Common column names
private static final String KEY_ID = "id";
private static final String KEY_CREATED_AT ="created_at";
//Profile column names
private static final String KEY_IMG = "img";
private static final String KEY_EMAIL = "email";
private static final String KEY_NAME = "name";
private static final String KEY_PHONE = "phone";
//Category column names
private static final String KEY_CATEGORY ="category";
//Card column names
private static final String KEY_CARD_IMG = "card_img";
private static final String KEY_CARD_NAME = "card_name";
private static final String KEY_CARD_TYPE_ID ="card_type_id";
public DatabaseHandler(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
//Category table create statement
private static final String CREATE_TABLE_CATEGORY = "CREATE TABLE " + TABLE_CATEGORY
+ "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_CATEGORY + " TEXT,"
+ KEY_CREATED_AT + " DATETIME" + ")";
//Card table create statement
private static final String CREATE_TABLE_CARD = "CREATE TABLE " + TABLE_CARD
+ "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_CARD_IMG + " BLOB," +
KEY_CARD_NAME + " TEXT," + KEY_CARD_TYPE_ID + " INTEGER," + KEY_CREATED_AT + " DATETIME" + ")";
//Profile table create statement
private static final String CREATE_TABLE_PROFILE = "CREATE TABLE " + TABLE_PROFILE +
"(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_IMG + " BLOB," +
KEY_EMAIL + " TEXT," + KEY_NAME + " TEXT," + KEY_PHONE + " TEXT" + ")";
@Override
public void onCreate(SQLiteDatabase db){
db.execSQL(CREATE_TABLE_CATEGORY);
db.execSQL(CREATE_TABLE_PROFILE);
db.execSQL(CREATE_TABLE_CARD);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVesion, int newVersion){
db.execSQL("DROP TABLE IF EXIST " + TABLE_PROFILE);
db.execSQL("DROP TABLE IF EXIST " + TABLE_CATEGORY);
db.execSQL("DROP TABLE IF EXIST " + TABLE_CARD);
onCreate(db);
}
//Profile
public void addProfile(Profile profile){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_IMG, profile.getImg());
values.put(KEY_EMAIL, profile.getEmail());
values.put(KEY_NAME, profile.getName());
values.put(KEY_PHONE, profile.getPhone());
db.insert(TABLE_PROFILE, null, values);
db.close();
}
public Profile getProfile(int id){
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_PROFILE, new String[]{KEY_ID, KEY_IMG, KEY_EMAIL, KEY_NAME, KEY_PHONE},
KEY_ID + "=?", new String[]{String.valueOf(id)}, null, null, null, null);
if(cursor!=null)
cursor.moveToFirst();
Profile profile = new Profile(Integer.parseInt(cursor.getString(0)), cursor.getBlob(1),
cursor.getString(2), cursor.getString(3), cursor.getString(4));
return profile;
}
public List<Profile> getAllProfile(){
List<Profile> profileList = new ArrayList<Profile>();
String selectQuery = "SELECT * FROM " + TABLE_PROFILE;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if(cursor.moveToFirst())
{
do{
Profile profile = new Profile();
profile.setID(Integer.parseInt(cursor.getString(0)));
profile.setImg(cursor.getBlob(1));
profile.setEmail(cursor.getString(2));
profile.setName(cursor.getString(3));
profile.setPhone(cursor.getString(4));
profileList.add(profile);
}while(cursor.moveToNext());
}
return profileList;
}
public int getProfileCounts(){
int count = 0;
String countQuery = "SELECT * FROM " + TABLE_PROFILE;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
if(cursor != null && !cursor.isClosed()){
count = cursor.getCount();
cursor.close();
}
return count;
}
public int updateProfile(Profile profile){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_IMG, profile.getImg());
values.put(KEY_EMAIL, profile.getEmail());
values.put(KEY_NAME, profile.getName());
values.put(KEY_PHONE, profile.getPhone());
return db.update(TABLE_PROFILE, values, KEY_ID + " = ?", new String[]{String.valueOf(profile.getID())});
}
public void updatePicture(byte[] imageInByte, int id){
SQLiteDatabase db = this.getWritableDatabase();
String strSQL = "UPDATE profile SET img = '"+imageInByte+"' WHERE id = " +id+"";
db.execSQL(strSQL);
db.close();
}
public void deleteProfile(Profile profile){
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_PROFILE, KEY_ID + "=?", new String[]{String.valueOf(profile.getID())});
db.close();
}
//Category
public void addCategory(Category category){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_CATEGORY, category.getCategory());
values.put(KEY_CREATED_AT, getDateTime());
db.insert(TABLE_CATEGORY, null, values);
db.close();
}
public Category getCategory(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_CATEGORY, new String[] { KEY_ID,
KEY_CATEGORY}, KEY_ID + "=?", new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Category category = new Category(Integer.parseInt(cursor.getString(0)),
cursor.getString(1));
// return contact
return category;
}
public List<Category> getAllCategory(){
List<Category> categoryList = new ArrayList<Category>();
String selectQuery = "SELECT * FROM " + TABLE_CATEGORY;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if(cursor.moveToFirst()){
do{
Category category = new Category();
category.setId(Integer.parseInt(cursor.getString(0)));
category.setCategory(cursor.getString(1));
category.setCreatedAt(cursor.getString(2));
categoryList.add(category);
}while(cursor.moveToNext());
}
return categoryList;
}
public int updateCategory(Category category) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_CATEGORY, category.getCategory());
return db.update(TABLE_CATEGORY, values, KEY_ID + " = ?", new String[] { String.valueOf(category.getId())});
}
public void deleteCategory(Category category) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_CATEGORY, KEY_ID + " = ?", new String[] { String.valueOf(category.getId())});
db.close();
}
public int getCategoryCount(){
String countQuery = "SELECT * FROM " + TABLE_CATEGORY;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
return cursor.getCount();
}
//Card
public void addCard(Card card){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_CARD_IMG, card.getCardImg());
values.put(KEY_CARD_NAME, card.getCardName());
values.put(KEY_CARD_TYPE_ID, Integer.toString(card.getCardTypeId()));
values.put(KEY_CREATED_AT, getDateTime());
db.insert(TABLE_CARD, null, values);
db.close();
}
public List<Card> getAllCardWithCategory(int categoryId){
List<Card> cardList = new ArrayList<Card>();
String selectQuery = "SELECT * FROM " + TABLE_CARD + " WHERE " + KEY_CARD_TYPE_ID + "=" + String.valueOf(categoryId) + ";";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if(cursor.moveToFirst()){
do{
Card card = new Card();
card.setCardId(Integer.parseInt(cursor.getString(0)));
card.setCardImg(cursor.getBlob(1));
card.setCardName(cursor.getString(2));
card.setCardTypeId(Integer.parseInt(cursor.getString(3)));
card.setCreatedAt(cursor.getString(4));
cardList.add(card);
}while(cursor.moveToLast());
}
return cardList;
}
public List<Card> getAllCard(){
List<Card> cardList = new ArrayList<Card>();
String selectQuery = "SELECT * FROM " + TABLE_CARD;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if(cursor.moveToFirst()){
do{
Card card = new Card();
card.setCardId(Integer.parseInt(cursor.getString(cursor.getColumnIndex(KEY_ID))));
card.setCardImg(cursor.getBlob(cursor.getColumnIndex(KEY_CARD_IMG)));
card.setCardName(cursor.getString(cursor.getColumnIndex(KEY_CARD_NAME)));
card.setCardTypeId(Integer.parseInt(cursor.getString(cursor.getColumnIndex(KEY_CARD_TYPE_ID))));
card.setCreatedAt(cursor.getString(cursor.getColumnIndex(KEY_CREATED_AT)));
cardList.add(card);
}while(cursor.moveToLast());
}
return cardList;
}
//getDateTime function
private String getDateTime() {
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss", Locale.getDefault());
Date date = new Date();
return dateFormat.format(date);
}
}
当我删除db.addCard方法时,我的程序工作,但我无法将图像添加到数据库中。我的程序的另一个片段是存储个人资料图片和信息,这是否是由于内存不足?
抱歉我的英语不好,欢迎任何帮助!感谢