在android中从SQLite数据库获取图像

在android中从SQLite数据库获取图像,android,database,sqlite,Android,Database,Sqlite,我无法从数据库中获取图像,请查看我的databasehelper,它是我的fetchimgString I和showmethView w类的MainActivity 我在这里实际做的是首先用相机拍摄图像,然后将其临时粘贴到第一个ImageView上,然后单击save按钮将其保存到数据库中还没有问题。但当我单击show按钮时,它必须通过将引用id作为textview的id在第二个ImageView中显示图像,但它没有显示图像,即showmethView无法从数据库中获取图像 我的主要活动 我的数

我无法从数据库中获取图像,请查看我的databasehelper,它是我的fetchimgString I和showmethView w类的MainActivity

我在这里实际做的是首先用相机拍摄图像,然后将其临时粘贴到第一个ImageView上,然后单击save按钮将其保存到数据库中还没有问题。但当我单击show按钮时,它必须通过将引用id作为textview的id在第二个ImageView中显示图像,但它没有显示图像,即showmethView无法从数据库中获取图像

我的主要活动

我的数据库文件:

包com.example.expnewbutton; 导入java.io.ByteArrayOutputStream; 导入android.content.ContentValues; 导入android.content.Context; 导入android.database.Cursor; 导入android.database.SQLException; 导入android.database.sqlite.SQLiteDatabase; 导入android.database.sqlite.SQLiteOpenHelper; 导入android.graphics.Bitmap; 导入android.graphics.Bitmap.CompressFormat; 导入android.graphics.BitmapFactory; 导入android.util.Log; 公共类databasehelper扩展了SQLiteOpenHelper{ 最终静态字符串databasename=Image; 最终静态int数据库版本=1; 位图bmp=null; 公共数据库HelperContext ctx{ superctx,databasename,null,databaseversion; } @凌驾 public void onCreateSQLiteDatabase db{ 试一试{ Log.dtag4545,数据库; db.execSQLcreate table mypicid text,pic BLOB; }捕获SQLE异常{ e、 打印跟踪; } } @凌驾 public void ONUPGRADESQLITEDB数据库,int-oldVersion,int-newVersion{ db.execSQLdrop表(如果存在mypic); //db.execSQLdrop表(如果存在); onCreatedb; } 公共长插入字符串id,位图img{ SQLiteDatabase=getwriteabledatabase; byte[]data=getBitmapAsByteArrayimg;//这是一个函数 ContentValues值=新的ContentValues; value.putid,id; value.putpic,数据; long a=base.insertmypic,null,value; 返回a; } 公共静态字节[]getBitmapAsByteArrayBitmap位图{ ByteArrayOutputStream outputStream=新建ByteArrayOutputStream; bitmap.compressformat.PNG,0,outputStream; 返回outputStream.toByteArray; } 公共布尔值fetchimgString i{ SQLiteDatabase=getReadableDatabase; 游标cs=null; 试一试{ cs=base.queryType,新字符串[]{pic},id=?, 新字符串[]{i},null,null,null; /* *字符串qu=从mypic中选择pic,其中id+i; * *游标cs=null;请尝试{cs=base.rawQueryqu,null;} *catchException e{return false;} */ 如果cs!=null{ cs.moveToFirst; 字节[]imgbyte=cs.getBlob0; cs.close; bmp=BitmapFactory.decodeByteArrayimgbyte,0,imgbyte.length; 返回true; } 否则{ 返回false; } } 捕获异常e{ 返回false; } } public void deleterecordString pe_id{ SQLiteDatabase=getwriteabledatabase; base.deletemypic,id=?,新字符串[]{pe_id}; } } 我的xml文件:

请帮我编码,我很快就需要。多谢各位


因此,问题可能在于数据库查询。我尝试使用两种方法进行查询,其中我隐藏了或没有隐藏,或者在代码中使用BLOB。请帮助我查找错误

您正在传递字符串i,需要传递i的值

您应该将查询更改为:

    cs = base.query("mypic", new String[] { "pic" }, "id=?",
            new String[] { String.valueOf(i) }, null, null, null);

谢谢你,哈尔先生,你的回答是的,这就是问题所在。但是你能告诉我为什么这个代码行在运行时没有从我的databasehelper类的deleterecordString pe_id中的string pe_id中获取值吗。代码行:base.deletemypic,id=?,新字符串[]{pe_id};该命令看起来是正确的,但前提是pe_id包含正确的值。也许pe_id不是你想象的那样。是的,我会看看的。谢谢
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/gb2"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText2"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="32dp"
        android:layout_toRightOf="@+id/textView2"
        android:onClick="cammethod"
        android:text="Cam" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button1"
        android:layout_marginLeft="46dp"
        android:layout_toRightOf="@+id/button1"
        android:onClick="insertimg"
        android:text="Save" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button2"
        android:layout_alignParentRight="true"
        android:layout_marginRight="22dp"
        android:text="Photo"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/text"
        android:layout_toRightOf="@id/imageView1"
        android:onClick="showmeth"
        android:text="show" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/imageView1" />

</RelativeLayout>
    cs = base.query("mypic", new String[] { "pic" }, "id=?",
            new String[] { String.valueOf(i) }, null, null, null);