我正在尝试从SQLite数据库插入和检索图像数据。 我有以下代码。正确插入图像,但是当我尝试检索时,Logcat显示--- SkImageDecoder :: Factory返回null错误。请帮帮我
此类用于创建和插入数据库
ImageView img;
String picpath;
Bitmap bmp;
Canvas cnvs;
Random r;
int x, y, height, width;
ByteArrayOutputStream blob;
//SQL sql;
Paint paint;
String name, no;
Uri data;
SQLiteDatabase db;
String[] filePath = {MediaStore.Images.Media.DATA};
;
Cursor cur;
TextView txt;
int colIndex;
ContentValues ctv;
byte[] imgData;
public static final String DB_NAME = "New.db";
public static final String TABLE_NAME = "Sample";
public static final int DB_VERSION = 1;
public static final String NAME = "Name";
public static final String NO = "No";
public static final String IMG = "Img";
public static final String PIXELX = "PosX";
public static final String PIXELY = "PosY";
public void create() {
db = openOrCreateDatabase(DB_NAME, Context.MODE_WORLD_WRITEABLE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME + "(" + NAME + " TEXT NOT NULL," + NO + " TEXT PRIMARY KEY," + IMG + " BLOB," + PIXELX + " INTEGER ," + PIXELY + " INTEGER );");
db.close();
}
public void insert() {
db = openOrCreateDatabase(DB_NAME, Context.MODE_PRIVATE, null);
db.execSQL("DELETE FROM " + TABLE_NAME);
ctv.put(NAME, name);
ctv.put(NO, no);
try {
FileInputStream img_file = new FileInputStream(picpath);
BufferedInputStream bis = new BufferedInputStream(img_file);
imgData = new byte[bis.available()];
bis.read();
ctv.put(IMG, imgData);
long val = db.insert(TABLE_NAME, null, ctv);
Toast.makeText(this.getBaseContext(), "Image Saved", Toast.LENGTH_SHORT).show();
if (val < 0)
Toast.makeText(this.getBaseContext(), "Image not Saved", Toast.LENGTH_SHORT).show();
bis.close();
}
catch (Exception e) {
e.getMessage();
}
db.close();
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
ctv = new ContentValues();
img = (ImageView) findViewById(R.id.imageView1);
r = new Random();
paint = new Paint();
x = r.nextInt(100);
blob = new ByteArrayOutputStream();
create();
Intent i = getIntent();
name = i.getStringExtra("name");
no = i.getStringExtra("no");
//Open Button
Button open = (Button) findViewById(R.id.button3);
open.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent gal_open = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(gal_open, 1);
}
});
txt = (TextView) findViewById(R.id.textView1);
Button shuffle = (Button) findViewById(R.id.button2);
shuffle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
bmp = Bitmap.createBitmap(img.getHeight(), img.getWidth(), Bitmap.Config.RGB_565);
cnvs = new Canvas(bmp);
height = img.getMeasuredHeight();
width = img.getMeasuredWidth();
x = r.nextInt(height);
y = r.nextInt(width);
paint.setColor(Color.RED);
txt.setText(x + "," + y);
cnvs.drawBitmap(BitmapFactory.decodeFile(picpath), 0, 0, null);
cnvs.drawRect(x, y, x + 20, y + 20, paint);
img.setImageBitmap(bmp);
}
});
Button ok = (Button) findViewById(R.id.button1);
txt = (TextView) findViewById(R.id.textView1);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
create();
insert();
startActivity(new Intent(getApplicationContext(), MainActivity.class));
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent intentData) {
super.onActivityResult(requestCode, resultCode, intentData);
if (requestCode == 1 && resultCode == RESULT_OK && intentData != null) {
bmp = Bitmap.createBitmap(img.getHeight(), img.getWidth(), Bitmap.Config.RGB_565);
cnvs = new Canvas(bmp);
height = img.getMeasuredHeight();
width = img.getMeasuredWidth();
x = r.nextInt(height);
y = r.nextInt(width);
paint.setColor(Color.RED);
data = intentData.getData();
cur = getContentResolver().query(data, filePath, null, null, null);
cur.moveToFirst();
colIndex = cur.getColumnIndex(filePath[0]);
picpath = cur.getString(colIndex);
cur.close();
txt = (TextView) findViewById(R.id.textView1);
txt.setText(x + "," + y);
cnvs.drawBitmap(BitmapFactory.decodeFile(picpath), 0, 0, null);
cnvs.drawRect(x, y, x + 20, y + 20, paint);
img.setImageBitmap(bmp);
}
}
检索数据的代码是
public class Login extends Activity {
//SQL sql;
User user;
String name;
TextView no;
ImageView img;
int imgx, imgy;
View touchlistener;
SQLiteDatabase db;
byte[] imgData1;
Cursor cur;
int pixelx, pixely;
public static final String DB_NAME = "New.db";
public static final String TABLE_NAME = "Sample";
public static final int DB_VERSION = 1;
public static final String NAME = "Name";
public static final String NO = "No";
public static final String IMG = "Img";
public static final String PIXELX = "PosX";
public static final String PIXELY = "PosY";
@SuppressLint("NewApi")
public void retrieve() {
img = (ImageView) findViewById(R.id.imageView1);
db = openOrCreateDatabase(DB_NAME, Context.MODE_WORLD_WRITEABLE, null);
cur = db.query(true, TABLE_NAME, new String[]{NAME, NO, IMG, PIXELX, PIXELY}, null, null, null, null, null, null, null);
cur.moveToFirst();
imgData1 = cur.getBlob(cur.getColumnIndex("Img"));
img.setImageBitmap(BitmapFactory.decodeByteArray(imgData1, 0, imgData1.length));
Toast.makeText(getApplicationContext(), "Image Retreived", 100).show();
cur.close();
db.close();
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
retrieve();
}
}