android.database.sqlite.SQLiteException: 未知错误 (Sqlite 代码 0):



您好,我的应用程序中遇到了这个奇怪的错误。应用程序第一次启动时,它会运行,但是当我存储来自相机的照片时,应用程序崩溃并显示此日志。应用从相机或图库中获取照片,并将其保存在数据库中,并在自定义网格视图中显示照片。

                 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.notepad.isidorosioannou.notepad/com.example.notepad.isidorosioannou.notepad.CameraMainActivity}: android.database.sqlite.SQLiteException: unknown error (Sqlite code 0): Native could not create new byte[], (OS error - 0:Success)
                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2444)
                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2504)
                     at android.app.ActivityThread.access$900(ActivityThread.java:165)
                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1368)
                     at android.os.Handler.dispatchMessage(Handler.java:102)
                     at android.os.Looper.loop(Looper.java:150)
                     at android.app.ActivityThread.main(ActivityThread.java:5546)
                     at java.lang.reflect.Method.invoke(Native Method)
                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)
                  Caused by: android.database.sqlite.SQLiteException: unknown error (Sqlite code 0): Native could not create new byte[], (OS error - 0:Success)
                     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.notepad.isidorosioannou.notepad.DatabaseAdapt.cursorToImage(DatabaseAdapt.java:179)
                     at com.example.notepad.isidorosioannou.notepad.DatabaseAdapt.loadAllImages(DatabaseAdapt.java:162)
                     at com.example.notepad.isidorosioannou.notepad.CameraMainActivity.onCreate(CameraMainActivity.java:48)

这是我的数据库助手的一部分代码,实际上是我在日志中得到错误的方法。

public static final String CAMERANOTE_CREATE = "create table " + CAMERANOTE_TABLE + " ( "
+ CAMERA_ID + " integer primary key autoincrement, "
+ CAMERA_TITLE + " text not null, "
+ CAMERA_DESC + " text not null, "
+ CAMERA_PATH + " blob);";    
public long createCamera(DataImage image){
ContentValues contentValues = new ContentValues();
contentValues.put(CAMERA_TITLE,image.getTitle());
contentValues.put(CAMERA_DESC,image.getDesc());
contentValues.put(CAMERA_PATH,image.getPath());
long insert= sqlDB.insert(CAMERANOTE_TABLE,null,contentValues);
return insert;
public ArrayList<DataImage> loadAllImages(){
ArrayList<DataImage> imageList= new ArrayList<>();
Cursor cursor = sqlDB.query(CAMERANOTE_TABLE,new String[]{CAMERA_ID,CAMERA_TITLE,CAMERA_DESC,CAMERA_PATH},null,null,null,null,null);
cursor.moveToFirst();
while (!cursor.isAfterLast()){
DataImage image = cursorToImage(cursor);
imageList.add(image);
}
cursor.close();
return imageList;
}
public Cursor loadAllTasks (){
Cursor cursor = sqlDB.query(TODOLIST_TABLE,new String[]{TODO_ID,TODO_TEXT,TODO_CHECKED},null,null,null,null,null);
return cursor;
}
private DataImage cursorToImage(Cursor cursor){
int id = cursor.getInt(cursor.getColumnIndex(DatabaseAdapt.CAMERA_ID));
String text = cursor.getString(cursor.getColumnIndex(DatabaseAdapt.CAMERA_TITLE));
String desc = cursor.getString(cursor.getColumnIndex(DatabaseAdapt.CAMERA_DESC));
byte [] path = cursor.getBlob(cursor.getColumnIndex(DatabaseAdapt.CAMERA_PATH));
DataImage image= new DataImage(id,text,desc,path);
return image;
}
}

这是我的主要活动:

public class CameraMainActivity extends AppCompatActivity implements View.OnClickListener {
private ImageView buttonImage;
private GridView gridView;
private ArrayList<DataImage> imageList;
public  DatabaseAdapt dbAdapter;
private CameraAdapter cameraAdapter;
private AlertDialog alertBuilder;
private Uri imageUri;
private Bitmap bitMap;
private byte [] byteArray;
private static final int REQUEST_CAMERA_CODE = 1;
private static final int REQUEST_GALLERY_CODE=2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar3);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
buttonImage = (ImageView)findViewById(R.id.cameraOptionButton);
buttonImage.setOnClickListener(this);
gridView = (GridView) findViewById(R.id.gridviewPhoto);
dbAdapter = new DatabaseAdapt(getApplicationContext());
dbAdapter.open();
imageList = new ArrayList<>();
imageList = dbAdapter.loadAllImages();
cameraAdapter = new CameraAdapter(this,imageList);
gridView.setAdapter(cameraAdapter);
createAlertWindow();
}
public void onDestroy(){
super.onDestroy();
dbAdapter.close();
}
@Override
public void onClick(View v) {
if (v.getId()==R.id.cameraOptionButton){
alertBuilder.show();
}
}
private void createAlertWindow(){
AlertDialog.Builder alertDialog = new  AlertDialog.Builder(this);
alertDialog.setTitle(R.string.alert_title)
.setItems(R.array.alert_dialog, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(which==0){
activateCamera();
}
else{
chooseFromGallery();
}
}
});
alertBuilder = alertDialog.create();
}
private void activateCamera(){
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent,REQUEST_CAMERA_CODE);
}
private void chooseFromGallery(){
Intent galleryIntent = new Intent(Intent.ACTION_PICK);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent,REQUEST_GALLERY_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_CAMERA_CODE && resultCode == RESULT_OK && data !=null){
Bundle extra = data.getExtras();
bitMap = extra.getParcelable("data");
byteArray = convertToByte(bitMap);
}
else if(requestCode==REQUEST_GALLERY_CODE && resultCode == RESULT_OK && data !=null){
imageUri = data.getData();
bitMap = decodeUri(imageUri,400);
byteArray=convertToByte(bitMap);
}
Intent intent = new Intent(this,CameraSaveActivity.class);
intent.putExtra("byteImage",byteArray);
startActivity(intent);
}
private Bitmap decodeUri(Uri image,int size){
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds=true;
BitmapFactory.decodeStream(getContentResolver().openInputStream(image),null,options);
int width = options.outWidth;
int height = options.outHeight;
int scale = 1;
while (true){
if(width/2<size || height/2<size){
break;
}
width /=2;
height /=2;
scale *=2;
}
BitmapFactory.Options options2 = new BitmapFactory.Options();
options2.inSampleSize=scale;
return BitmapFactory.decodeStream(getContentResolver().openInputStream(image),null,options2);
}catch (Exception e){
e.printStackTrace();
}
return null;
}
private  byte[] convertToByte(Bitmap bitmap){
ByteArrayOutputStream b = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG,100,b );
return  b.toByteArray();
}

}

这是我的第二个活动,我只是预览图像并保存它。

public class CameraSaveActivity extends AppCompatActivity implements View.OnClickListener {
private EditText cameraSaveTitle , cameraSaveDesc;
private ImageView cameraPreview;
private Button saveButton;
private byte [] byteArray;
private DatabaseAdapt adapt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera_save);
cameraSaveTitle = (EditText)findViewById(R.id.cameraEditTitle);
cameraSaveDesc = (EditText)findViewById(R.id.cameraEditDesc);
cameraPreview = (ImageView)findViewById(R.id.cameraPreview);
saveButton = (Button)findViewById(R.id.saveCameraButton);
Intent intent = getIntent();
byteArray = intent.getByteArrayExtra("byteImage");
ByteArrayInputStream imageStream = new ByteArrayInputStream(byteArray);
cameraPreview.setImageBitmap(BitmapFactory.decodeStream(imageStream));
adapt = new DatabaseAdapt(this);
adapt.open();
saveButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v.getId()==R.id.saveCameraButton){
String text1 = cameraSaveTitle.getText().toString();
String text2 = cameraSaveDesc.getText().toString();
DataImage image = new DataImage(text1,text2,byteArray);
adapt.createCamera(image);
finish();

}
}
public void onDestroy(){
super.onDestroy();
adapt.close();
}

}

有人可以帮忙吗?我搜索了,但找不到解决方案

您的问题是您将 path 存储在 CAMERA_PATH 中,这是字符串并尝试获取 Blob。存在类型不匹配,这就是引发错误的原因。 在尝试从CAMERA_PATH中检索 Blob 之前,需要在中插入 Blob。

最新更新