简体   繁体   English

无法从CursorWindow读取第0行,第0行

[英]Couldn't read row 0, col 0 from CursorWindow

java.lang.IllegalStateException: Couldn't read row 0, col 0 from CursorWindow. java.lang.IllegalStateException:无法从CursorWindow读取行0,列0。 Make sure the Cursor is initialized correctly before accessing data from it. 在从游标访问数据之前,请确保正确初始化了游标。

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);

                }
        }
    }

save image method it is succesfully instereted path into sqlite database 保存图像方法成功将路径插入到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 method for get image from inserted into database here i get the error mesg 从这里插入数据库获取图像的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();
            }
        }
    }
}

更改为此

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)";`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM