0

java.lang.IllegalStateException: Couldn't read row 0, col 0 from CursorWindow. 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

    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

    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();
            }
        }
    }
}
  • you can save the image path in your database or byte array string of that image. – kalpana c Oct 05 '16 at 09:33
  • How can insert imageview value from myprofile page to storagedata? – Manul Parekh Oct 05 '16 at 09:35
  • how you are setting value in imageview? is it in xml file? – kalpana c Oct 05 '16 at 09:42
  • It's always better to save image first in Application Folder and then save that file URL in database as string. Later on Just get that file url from database and use Picasso to load that image. Converting to byterarray and then again converting to bitmap takes of memory and your application will become unresponsive a couple of times in that case. – Kshitij Jain Oct 05 '16 at 09:42
  • OR you wil get the bitmap value from imageview, and get byte array. ex: imageView.buildDrawingCache(); Bitmap bmap = imageView.getDrawingCache(); ByteArrayOutputStream stream = new ByteArrayOutputStream(); bmap.compress(Bitmap.CompressFormat.PNG, 100, stream); byte[] byteArray = stream.toByteArray(); – kalpana c Oct 05 '16 at 09:44
  • Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath); i2.setImageBitmap(yourSelectedImage); here i set the Imageview image – Manul Parekh Oct 05 '16 at 09:46
  • kshitij jain sir can you give me some example link – Manul Parekh Oct 05 '16 at 09:47
  • we have not any "BLOG" data type ,first changed that to "BLOB" + KEY_IMAGE + " BLOB)"... – Nickan Oct 05 '16 at 12:20
  • Possible duplicate of [How to store(bitmap image) and retrieve image from sqlite database in android?](http://stackoverflow.com/questions/11790104/how-to-storebitmap-image-and-retrieve-image-from-sqlite-database-in-android) – OneCricketeer Oct 06 '16 at 07:14
  • How to get image from cursor? – Manul Parekh Oct 07 '16 at 09:26

1 Answers1

0

change to this `

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)";`
Prashant Sharma
  • 1,357
  • 1
  • 21
  • 31