Android 从Sqlite数据库访问图像
我的代码中有一个问题,我正在存储访问来自sqlite数据库的学生数据,其中包含学生姓名、年龄和图片。我完美地存储了所有这些数据,但无法仅访问图像字段 图像在屏幕上显示为空白 我不知道为什么。请检查我的代码并给我一些有用的建议 谢谢 以下是代码:Android 从Sqlite数据库访问图像,android,image,sqlite,Android,Image,Sqlite,我的代码中有一个问题,我正在存储访问来自sqlite数据库的学生数据,其中包含学生姓名、年龄和图片。我完美地存储了所有这些数据,但无法仅访问图像字段 图像在屏幕上显示为空白 我不知道为什么。请检查我的代码并给我一些有用的建议 谢谢 以下是代码: package com.ex.imageStore; import java.util.Locale; import android.app.Activity; import android.database.Cursor; import andro
package com.ex.imageStore;
import java.util.Locale;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class NewScreen extends Activity{
SQLiteDatabase db;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.newscreen);
TextView name = (TextView)findViewById(R.id.textView1);
TextView age = (TextView)findViewById(R.id.textView2);
ImageView img = (ImageView)findViewById(R.id.imageView1);
db = openOrCreateDatabase(
"StudentData.db"
, SQLiteDatabase.CREATE_IF_NECESSARY
, null
);
db.setVersion(1);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
Cursor cur = db.query("stuData",
null, null, null, null, null, null);
//cur.moveToFirst();
cur.moveToPosition(0);
name.setText(cur.getString(1));
age.setText(cur.getString(2));
byte[] bb = cur.getBlob(3);
Bitmap theImage = BitmapFactory.decodeByteArray(bb,0,bb.length);
img.setImageBitmap(theImage);//-->It //cannot convert bytearray to bitmap
/* while (cur.isAfterLast() == false) {
tv.append("n" + cur.getString(1));
cur.moveToNext();
}*/
cur.close();
}
}
图像变量始终指定为null………将
字节[]bb
转换为字节数组输入流。对于你的节目来说,那将是
ByteArrayInputStream is = new ByteArrayInputStream(bb);
Bitmap theImage = BitmapFactory.decodeStream(is);
ImageView img = (ImageView)findViewById(R.id.imageView1);
img.setImageBitmap(theImage);
我想这会对您有所帮助。将字节[]bb
转换为字节数组输入流。对于你的节目来说,那将是
ByteArrayInputStream is = new ByteArrayInputStream(bb);
Bitmap theImage = BitmapFactory.decodeStream(is);
ImageView img = (ImageView)findViewById(R.id.imageView1);
img.setImageBitmap(theImage);
我认为这将对您有所帮助。如何确保图像保存在数据库中?如何确保图像保存在数据库中?