我正在尝试在SQLite数据库中插入图像并从中检索图像。图像已成功插入,但是当我尝试检索图像时,应用程序崩溃,给出java.lang.IllegalStateException: Couldn't read row 0, col 1 from CursorWindow
。
错误:
Process: com.example.mastermind.image, PID: 12651
java.lang.IllegalStateException: Couldn't read row 0, col 1 from
CursorWindow. Make sure the Cursor is initialized correctly before
accessing data from it.
at android.database.CursorWindow.nativeGetBlob(Native Method)
at android.database.CursorWindow.getBlob(CursorWindow.java:403)
at android.database.AbstractWindowedCursor.getBlob(AbstractWindowedCursor.java:45)
at com.example.mastermind.image.MainActivity$3.onClick(MainActivity.java:96)
at android.view.View.performClick(View.java:6392)
at android.view.View$PerformClick.run(View.java:25133)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:198)
at android.app.ActivityThread.main(ActivityThread.java:7038)
at java.lang.reflect.Method.invoke(Native Method)
at
数据库类:
package com.example.mastermind.image;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MyDatabase extends SQLiteOpenHelper {
private static String TABLE_NAME = "ADI";
private static String COL_ID = "_ID";
private static String COL_IMG = "IMG";
public MyDatabase(Context context) {
super(context, "my.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + "( " + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT ," + COL_IMG + " BLOB NOT NULL );");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean addData(byte[] img) {
SQLiteDatabase db = getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("img", img);
long res = db.insert(TABLE_NAME, null, cv);
if (res == -1) {
return false;
}
return true;
}
public Cursor getData() throws SQLException {
SQLiteDatabase db = getWritableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE
" + COL_ID + " = '1'", null);
return cursor;
}
}
MainActivity:
package com.example.mastermind.image;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.ByteArrayOutputStream;
public class MainActivity extends AppCompatActivity {
ImageView img,
img1;
Button b1,
b2;
Bitmap currimg;
MyDatabase db;
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if ((requestCode == 1) && (resultCode == Activity.RESULT_OK)) {
Uri photouri = data.getData();
if (photouri != null) {
try {
currimg = MediaStore.Images.Media.getBitmap(getContentResolver(), photouri);
img.setImageBitmap(currimg);
} catch(Exception e) {
e.printStackTrace();
}
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new MyDatabase(this);
img = findViewById(R.id.img);
b1 = findViewById(R.id.b);
b2 = findViewById(R.id.b1);
img1 = findViewById(R.id.img1);
img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent gallary = new Intent(Intent.ACTION_GET_CONTENT);
gallary.setType("image/*");
startActivityForResult(gallary, 1);
}
});
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//addData
boolean flag = db.addData(makeimgByte(currimg));
if (flag) {
Toast.makeText(getApplicationContext(), "Inserted
Sucessfully", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
}
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//getData
Cursor cursor = db.getData();
if (cursor.moveToNext()) {
Bitmap bitmap = getImage(cursor.getBlob(1));
int s = cursor.getInt(0);
Toast.makeText(getApplicationContext(), "Sucess id=
" + s, Toast.LENGTH_SHORT).show();
img1.setImageBitmap(bitmap);
cursor.close();
}
}
});
}
public byte[] makeimgByte(Bitmap bitmap) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0, outputStream);
return outputStream.toByteArray();
}
public Bitmap getImage(byte[] img) {
Bitmap bitmap = BitmapFactory.decodeByteArray(img, 0, img.length);
return bitmap;
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:layout_width="250dp"
android:layout_height="250dp"
android:id="@+id/img"
android:layout_margin="16dp"
android:layout_gravity="center"
android:src="@drawable/ic_launcher_foreground"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b"
android:text="Add Data"
android:layout_gravity="center"
android:layout_margin="16dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b1"
android:text="Get Data"
android:layout_gravity="center"
android:layout_margin="16dp"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/img1"
/>
</LinearLayout>
我希望图像输出应在ID为img1的图像视图中显示。