我的列填充了byte [] aka image。我想拍摄该图像并将其添加到我的列表视图中,具体取决于我的行...我有一个XML文件,其中包含2个textView和一个imageView,这是我尝试将图像放入imageView .. < / p>
public class CBFilter extends ListActivity {
ListView RecipeNames;
Cursor cursor, cursor2;
SimpleCursorAdapter adapter;
CBDataBaseHelper data;
SQLiteDatabase data2;
TextView RecipeText, RowId;
String[] from = { CBDataBaseHelper.KEY_NAME };
int[] to = { R.id.row};
ImageView image;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
boolean diditwork;
try{
RecipeNames = (ListView) findViewById(android.R.id.list);
RecipeNames.setTextFilterEnabled(true);
image = (ImageView) findViewById(R.id.RecipeImage);
/* image.setImageBitmap(BitmapFactory.decodeByteArray(dataImage, 0, dataImage.length));*/
RecipeText = (TextView) findViewById(R.id.recipeText);
adapter = new SimpleCursorAdapter (this, 0, cursor, null, null);
image = (ImageView) findViewById(R.id.RecipeImage);
data = new CBDataBaseHelper(this);
data.open();
cursor = data.query();
startManagingCursor(cursor);
adapter = new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
RecipeNames.setAdapter(adapter);
adapter.notifyDataSetChanged();
byte[] blob=cursor.getBlob(4);
Bitmap bmp=BitmapFactory.decodeByteArray(blob,0,blob.length);
image=new ImageView(this);
image.setImageBitmap(bmp);
}catch(Exception e){
diditwork = false;
String error = e.toString();
Dialog d = new Dialog(this);
d.setTitle("darn");
TextView tv = new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();
}}
public void CreateNew(View view){
Intent myIntent = new Intent(this, CBCreate.class);
startActivity(myIntent);
}
@Override
public void onListItemClick(ListView parent, View v, int position, long id) {
super.onListItemClick(parent, v, position, id);
Intent intent1 = new Intent(this, CBCreate.class);
long rowId = cursor.getLong(cursor.getColumnIndex(CBDataBaseHelper.KEY_ROWID));
String s = String.valueOf(rowId);
intent1.putExtra("SELECTED", s);
startActivity(intent1);
}
}
}
确定这是我的DBHelper类,调用方法query()...
public class CBDataBaseHelper {
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "Recipe_Name";
public static final String KEY_CATEGORY = "Recipe_Category";
public static final String KEY_DESCRIPTION = "Recipe_Description";
public static final String KEY_IMAGE = "Recipe_Image";
public static final String KEY_ROWID2 = "_id";
public static final String fKEY_ROWID2 = "f_id";
public static final String KEY_NAME2 = "Ingredient_Name";
private static final String DATABASE_NAME = "Recipedb";
private static final String DATABASE_TABLE = "RecipeData";
private static final String DATABASE_TABLE2 = "IngredientData";
private static final int DATABASE_VERSION = 1;
private DBHelper myHelper;
private final Context mycontext;
private SQLiteDatabase mydatabase;
private static class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE + "( " + KEY_ROWID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME
+ " TEXT NOT NULL, " + KEY_CATEGORY + " TEXT NOT NULL,"
+ KEY_DESCRIPTION + " TEXT NOT NULL, "
+ KEY_IMAGE + " BLOB" + ") ");
/*db.execSQL("CREATE TABLE " + DATABASE_TABLE2 + "( " + KEY_ROWID2
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME2
+ " TEXT NOT NULL, " + fKEY_ROWID2 + " INTEGER , FOREIGN KEY "
+ "(" + fKEY_ROWID2 + ") " + "REFERENCES " + DATABASE_TABLE
+ "( " + KEY_ROWID + " ))");*/
db.execSQL("create table "
+ DATABASE_TABLE2 + " (" + KEY_ROWID2
+ " integer primary key autoincrement, " + KEY_NAME2
+ " text not null, " + fKEY_ROWID2 + " integer,"
+ " FOREIGN KEY ("+fKEY_ROWID2+") REFERENCES "+DATABASE_TABLE+" ("+KEY_ROWID+"));");
/*db.execSQL("CREATE TABLE " + DATABASE_TABLE2 + "( "
+ KEY_ROWID2 + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_NAME2 + " TEXT NOT NULL, "
+ fKEY_ROWID2 + " INTEGER , FOREIGN KEY " + "(" + fKEY_ROWID2 + ") " + "REFERENCES " + DATABASE_TABLE + " ( " + KEY_ROWID + " ))");
*/
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP IF EXISTS " + DATABASE_TABLE);
db.execSQL("DROP IF EXISTS " + DATABASE_TABLE2);
onCreate(db);
}
}
public CBDataBaseHelper(Context c){
mycontext = c;
}
public CBDataBaseHelper open() throws SQLException{
myHelper = new DBHelper(mycontext);
mydatabase = myHelper.getWritableDatabase();
return this;
}
public void close(){
myHelper.close();
}
public long createEntry(String name, String description, String category){
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, name);
cv.put(KEY_CATEGORY, description);
cv.put(KEY_DESCRIPTION, category);
return mydatabase.insert(DATABASE_TABLE, null, cv);
}
public long createEntry2(String name, Long fId){
ContentValues cv = new ContentValues();
cv.put(KEY_NAME2, name);
cv.put(fKEY_ROWID2, fId);
return mydatabase.insert(DATABASE_TABLE2, null, cv);
}
public Cursor query() {
// Open Database
String[] columns = new String[]{KEY_ROWID, KEY_NAME, KEY_CATEGORY, KEY_DESCRIPTION, KEY_IMAGE};
Cursor cursor = mydatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}
public Cursor IngredientfetchRows(long frowId) throws SQLException {
Cursor mCursor = mydatabase.query(true, DATABASE_TABLE2, new String[] { KEY_ROWID2, KEY_NAME2,
fKEY_ROWID2 }, fKEY_ROWID2 + "="
+ frowId, null, null, null, null, null);
return mCursor;
}
public Cursor IngredientfetchRow(long rowId) throws SQLException {
Cursor mCursor = mydatabase.query(true, DATABASE_TABLE2, new String[] { KEY_ROWID2, KEY_NAME2,
fKEY_ROWID2 }, KEY_ROWID2 + "="
+ rowId, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public Cursor fetchRow(long rowId) throws SQLException {
Cursor mCursor = mydatabase.query(true, DATABASE_TABLE, new String[] { KEY_ROWID, KEY_NAME,
KEY_CATEGORY, KEY_DESCRIPTION }, KEY_ROWID + "="
+ rowId, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public boolean updateRecipe(long rowId, String RecipeName,
String RecipeCategory, String RecipeDescription)
{
ContentValues args = new ContentValues();
args.put(KEY_NAME, RecipeName);
args.put(KEY_DESCRIPTION, RecipeDescription);
args.put(KEY_CATEGORY, RecipeCategory);
return mydatabase.update(DATABASE_TABLE, args,
KEY_ROWID + "=" + rowId, null) > 0;
}
public boolean updateRecipe2(long rowId, String IngredientName)
{
ContentValues args = new ContentValues();
args.put(KEY_NAME2, IngredientName);
return mydatabase.update(DATABASE_TABLE2, args,
KEY_ROWID2 + "=" + rowId, null) > 0;
}
public boolean updateRecipe3(long rowId, byte[] path)
{
ContentValues args = new ContentValues();
args.put(KEY_IMAGE, path);
return mydatabase.update(DATABASE_TABLE, args,
KEY_ROWID + "=" + rowId, null) > 0;
}
}
然后我有相机api将字节信息保存到RecipeImage列。
public class CameraAPI extends Activity implements SurfaceHolder.Callback{
public Camera camera;
MediaRecorder mediaRecorder;
CBDataBaseHelper RH;
TextView RecipeID;
public void onCreate (Bundle savedInstanceState){
boolean diditwork;
try{
super.onCreate(savedInstanceState);
setContentView(R.layout.camera);
RecipeID = (TextView)findViewById(R.id.Rid2);
String RowID;
Bundle extras = getIntent().getExtras();
RowID = extras.getString("SELECTED2");
RecipeID.setText(RowID);
SurfaceView surface = (SurfaceView)findViewById(R.id.acccam);
SurfaceHolder holder = surface.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}catch(Exception e){
diditwork = false;
String error = e.toString();
Dialog d = new Dialog(this);
d.setTitle("darn");
TextView tv = new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();
}}
public void takePhoto(View view){
//String ID = RecipeID.getText().toString();
//Long LID = Long.parseLong(ID);
boolean diditwork;
try{
takePicture();
}catch(Exception e){
diditwork = false;
String error = e.toString();
Dialog d = new Dialog(this);
d.setTitle("darn");
TextView tv = new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();
}
//String path = "/sdcard/Image.jpg";
//RH.updateRecipe3(LID,path);
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
if (mediaRecorder == null){
try{
camera = camera.open();
camera.setPreviewDisplay(holder);
camera.startPreview();
}catch (IOException e){
Log.d("CAMERA", e.getMessage());
}
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
camera.stopPreview();
camera.release();
}
public void takePicture(){
boolean diditwork;
try{
camera.takePicture(shutterCallback, rawCallback, jpegCallback);
}catch(Exception e){
diditwork = false;
String error = e.toString();
Dialog d = new Dialog(this);
d.setTitle("darn");
TextView tv = new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();
}
}
ShutterCallback shutterCallback= new ShutterCallback()
{
public void onShutter(){
}
};
PictureCallback rawCallback = new PictureCallback(){
public void onPictureTaken(byte[] data, Camera camera){
}
};
PictureCallback jpegCallback = new PictureCallback(){
public void onPictureTaken(byte[] data, Camera camera){
FileOutputStream outStream = null;
try{
outStream = new FileOutputStream("/sdcard/Image.jpg");
outStream.write(data);
outStream.close();
}catch(FileNotFoundException e){
Log.d("CAMERA", e.getMessage());
}catch(IOException e){
Log.d("CAMERA",e.getMessage());
}
RH = new CBDataBaseHelper(CameraAPI.this);
RH.open();
String ID = RecipeID.getText().toString();
Long RID = Long.parseLong(ID);
RH.updateRecipe3(RID, data);
RH.close();
}
};
}
当我尝试将字节数组转换为blob时,我得到的错误是一个空点异常,这里有什么帮助吗?
谢谢
Stefan