java.lang.IllegalStateException:无法从CursorWindow读取第0行,第0列。在从中访问数据之前,请确保Cursor已正确初始化。
public class MyProfile extends AppCompatActivity {
private Context mContext;
private ImageView i1, i2,i3;
private static final int SELECT_IMAGE = 1;
private StoreProfileData mStore;
SQLiteDatabase db;
String path;
Cursor cursor,c;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_profile);
mContext = MyProfile.this;
i1 = (ImageView) findViewById(R.id.user_profile_photo);
i2 = (ImageView) findViewById(R.id.user_profile_photo_hidden);
i3 = (ImageView) findViewById(R.id.header_cover_image);
db=this.openOrCreateDatabase("test.db",Context.MODE_PRIVATE,null);
db.execSQL("create table if not exists tb(a blob)");
}
public void initialize() {
i2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
final int ACTIVITY_SELECT_IMAGE = 1234;
startActivityForResult(i, ACTIVITY_SELECT_IMAGE);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1234:
if (resultCode == RESULT_OK) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
File f = new File(filePath);
path=f.getPath();
Log.d("path",path);
cursor.close();
// Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
// i2.setImageBitmap(yourSelectedImage);
}
}
}
保存图像方法,它成功地进入了路径 sqlite数据库
public void saveImage(View view)
{
try {
FileInputStream fts = null;
fts = new FileInputStream(path);
byte[]image=new byte[fts.available()];
fts.read(image);
ContentValues values=new ContentValues();
values.put("a",image);
db.insert("tb", null, values);
fts.close();
Toast.makeText(this,"inserted",Toast.LENGTH_LONG).show();
}
catch (IOException e){
e.printStackTrace();
}
}
获取图像的getImage方法从这里插入数据库我得到错误消息
public void getImage(View view) {
cursor = db.rawQuery("select * from tb", null);
Log.d("dv", String.valueOf(cursor));
if (cursor.getCount() > 0) {
Log.d("crsr", "sdds");
if (cursor.moveToNext()) {
byte[] image = cursor.getBlob(0);
Bitmap bmp = BitmapFactory.decodeByteArray(image, 0, image.length);
i2.setImageBitmap(bmp);
Toast.makeText(this, "selected", Toast.LENGTH_LONG).show();
}
}
}
}
答案 0 :(得分:0)
改为这个`
private static String IMAGE_CREATE_QUERY = "CREATE TABLE " + IMAGE_TABLE_NAME + " (" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + KEY_EMAIL + " TEXT," + KEY_NUMBER + " TEXT," + KEY_CLASS + " TEXT," + KEY_STREAM + " TEXT," + KEY_IMAGE + " BLOB)";`