Android 如何保存从相机拍摄的图像并将其显示到listview-与“崩溃”;“非法国家例外”;
我不知道该怎么做 对于我使用的数据库:Android 如何保存从相机拍摄的图像并将其显示到listview-与“崩溃”;“非法国家例外”;,android,sqlite,listview,Android,Sqlite,Listview,我不知道该怎么做 对于我使用的数据库: public void insertImage(byte[] bytes) { ContentValues cv = new ContentValues(); cv.put("imageblob", bytes); Log.e("inserted", "inserted"); dbHelper.getWritableDatabase().insert("Image", "imageb
public void insertImage(byte[] bytes) {
ContentValues cv = new ContentValues();
cv.put("imageblob", bytes);
Log.e("inserted", "inserted");
dbHelper.getWritableDatabase().insert("Image", "imageblob", cv);
}
public byte[] getImage(Cursor c) {
return (c.getBlob(1));
}
数据库帮助程序:
...
public static final String KEY_COMMENTS = "comments";
public static final String KEY_IMAGE = "imageblob";
private static final String SCRIPT_CREATE_DATABASE = "create table "
+ DATABASE_TABLE + " (" + KEY_ROWID
+ " integer primary key autoincrement, " + KEY_TITLE
+ " text not null, ......+KEY_COMMENTS+" text not null,"+KEY_IMAGE+" imageblob BLOB);";
我的项目:
public class myItems {
....
String comments;
byte[] imageblob;
public byte[] getImage() {
return imageblob;
}
public void setImage(byte[] imageblob) {
this.imageblob = imageblob;
}
现在,我在MainActivity中有了listview。在另一个活动ShowList中,我有一个用来拍照的按钮:
case R.id.photobtn:
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
break;
在onActivityResult中:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == CAMERA_REQUEST){
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
sqlHandler.insertImage(byteArray);
}
我的适配器:
公共类myAdapter扩展了BaseAdapter{
...
public myAdapter(Context context, ArrayList<myItems> list) {
this.context = context;
this.setItems(list);
}
@Override
public View getView(int position, View convertView, ViewGroup arg2) {
myItems theItems = getItems().get(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.showadapterlist, null);
}
...
TextView Comments = (TextView) convertView.findViewById(R.id.text_comments);
Comments.setText(theItems.getComments());
ImageView myImage=(ImageView) convertView.findViewById(R.id.myimage);
myImage.setImageBitmap(getImageFromBLOB(theItems.getImage()));
return convertView;
}
public static Bitmap getImageFromBLOB(byte[] mBlob)
{
byte[] bb = mBlob;
return BitmapFactory.decodeByteArray(bb, 0, bb.length);
}
public ArrayList<myItems> getItems() {
return items;
}
public void setItems(ArrayList<myItems> items) {
this.items = items;
}
我应该如何处理“imageblob”值
2) 在MainActivity(列表视图所在的位置)中,我有:
我应该如何处理imageblob
现在,当我启动我的应用程序时,在第行出现了“IllegalStateException”崩溃
“字节[]blob=c1.getBlob(c1.getColumnIndex(“imageblob”);”
.如果我尝试c1.setImage,它将不起作用
------------------新的--------------------------------------------------
在一项活动中:
case R.id.photobtn:
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
break;
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == CAMERA_REQUEST){
if (resultCode==RESULT_OK){
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, stream);
blobvalue = stream.toByteArray();
Bundle extras = new Bundle();
Intent k=new Intent(this,MainActivity.class);
extras.putParcelable("Bitmap", photo);
k.putExtras(extras);
}
}
if (resultCode == RESULT_CANCELED) {
}
}
在适配器中:
public View getView(int position, View convertView, ViewGroup arg2) {
myItems theItems = getItems().get(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.showadapterlist, null);
}
ImageView myImage=(ImageView) convertView.findViewById(R.id.myimage);
final Bitmap image;
if(theItems.getImagemyItems() != null)
{
byte []temp = theItems.getImagemyItems();
image = BitmapFactory.decodeByteArray(temp, 0, temp.length);
myImage.setImageBitmap(image);
}
else
{
image = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
myImage.setImageBitmap(image);
}
在myItems中:
public class myItems {
String id;
...
byte[] imageblob;
public String getID() {
return id;
}
....
}
根据您创建的数据库语句
private static final String SCRIPT_CREATE_DATABASE = "create table "
+ DATABASE_TABLE + " (" + KEY_ROWID
+ " integer primary key autoincrement, " + KEY_TITLE
+ " text not null, ......+KEY_COMMENTS+" text not null,"+KEY_IMAGE+" imageblob BLOB);";
有人提到你
"+KEY_IMAGE+" imageblob BLOB
因此,列值在语法上是“imageblob imageblol BLOB”,这不是正确的sql语句。
因此,只要从那里删除“imageblob”并尝试一下,这可能会解决您的问题。…您通常从不将图像/位图插入数据库。将它们放在sd卡上,并将其路径存储在数据库中。如果您将图像存储在数据库中,它们将存储为“Blob”。我可能是错的,但您尝试使用的方法是
设置图像(字符串文件名)
。您不是以字符串形式传递图像,而是传递其位置的字符串。@George Hi..,这里的第一条注释(M-WajeEh的注释)适用于sure@Abhi:您好,我想我正试图在我的代码中实现这一点,对吗?(对不起,我没有经验……)谢谢!现在卸载上一个应用程序并尝试再次运行…因为数据库已经存在!!!所以我们需要删除它…:好的,现在应用程序运行了,但是我当然在listview中没有照片。我正在尝试这个“byte[]imageView=c1.getBlob(c1.getColumnIndex(“imageblob”);ByteArrayInputStream=new ByteArrayInputStream”(imageView);Bitmap theImage=BitmapFactory.decodeStream(inputStream);“但是我正在尝试这个”imageView.setImageBitmap(theImage);“并说“不能在数组类型byte[]上调用..”。另外,请检查上面的myItems和myAdapter类。我不确定要准确放置什么。谢谢!byte[]temp=c.getBlob(3);位图image=BitmapFactory.decodeByteArray(temp,0,temp.length);BitmapDrawable dd=new BitmapDrawable(image.getImage());imgView.setBackgroundDrawable(dd);尝试使用此方法而不是代码,并检查您创建的数据库文件是否存储了blob……数据库位于data/data//databases/…:它显示类型位图的“方法getImage()”未定义,“方法setBackgroundDrawable已弃用”字节[]temp=c.getBlob(3);位图image=BitmapFactory.decodeByteArray(temp,0,temp.length);imageview.setImageBitmap(image);尝试此操作…并检查该位置的db。。。。
private static final String SCRIPT_CREATE_DATABASE = "create table "
+ DATABASE_TABLE + " (" + KEY_ROWID
+ " integer primary key autoincrement, " + KEY_TITLE
+ " text not null, ......+KEY_COMMENTS+" text not null,"+KEY_IMAGE+" imageblob BLOB);";
"+KEY_IMAGE+" imageblob BLOB