从 Sqlite 数据库访问图像
我的代码中有一个问题,我正在存储访问来自 sqlite 数据库的学生数据(包含学生姓名、年龄和图片)。我完美地存储了所有这些数据,但无法仅访问图像字段。
图像在屏幕上显示空白,
我不知道为什么。请检查我的代码并给我有用的建议。
谢谢..
这是代码:
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();
}
}
theImage 变量始终分配为 null.........
I have a problem in my code that I am storing accessing the student data from sqlite database with student name, age and picture. I am storing all these data perfectly but cannot access the image field only.
Image shows blank on the screen,
I don't know why. Please Review my code and give me useful suggestions.
Thank You..
Here is Code:
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();
}
}
theImage variable always assigned as null.........
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
byte[] bb
转换为ByteArrayInputStream
。对于您的程序,我认为这会对您有所帮助。
Convert the
byte[] bb
to aByteArrayInputStream
. For your program, that would beI think this will help you.