【问题标题】:Couldn't read row 0, col 0 from CursorWindow无法从 CursorWindow 读取第 0 行第 0 列
【发布时间】:2017-02-13 16:22:13
【问题描述】:

java.lang.IllegalStateException:无法从 CursorWindow 读取第 0 行第 0 列。确保在从光标访问数据之前正确初始化光标。

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

【问题讨论】:

  • 您可以将图像路径保存在数据库中或该图像的字节数组字符串中。
  • 如何将 myprofile 页面中的 imageview 值插入 storagedata?
  • 您如何在 imageview 中设置值?它在xml文件中吗?
  • 最好先将图像保存在应用程序文件夹中,然后将该文件 URL 作为字符串保存在数据库中。稍后只需从数据库中获取该文件 url 并使用 Picasso 加载该图像。转换为字节数组,然后再次转换为位图会占用内存,在这种情况下,您的应用程序将多次无响应。
  • 或者你会从 imageview 获取位图值,并获取字节数组。例如:imageView.buildDrawingCache();位图 bmap = imageView.getDrawingCache(); ByteArrayOutputStream 流 = 新的 ByteArrayOutputStream(); bmap.compress(Bitmap.CompressFormat.PNG, 100, 流); byte[] byteArray = stream.toByteArray();

标签: java android sqlite imageview byte


【解决方案1】:

改成这个`

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

【讨论】:

  • 先生如何插入imageview值并在imageview中检索它?
  • 为什么要把图片存入数据库?
  • 将图像添加到应用程序文件夹中。保存 url 并在需要时从该 url 获取图像
猜你喜欢
  • 2017-05-22
  • 2014-02-21
  • 2013-05-06
  • 2020-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多