Nullpointer异常将图像保存到sqlite数据库中

时间:2015-05-13 10:50:37

标签: android sqlite android-image android-bitmap

我想用blob在sqlite数据库中保存图像。但如果我点击我的按钮保存新条目(标题和图片),我会收到以下问题:

java.lang.NullPointerException

以下行:

        bitmapGlobal = ((BitmapDrawable) myImage1.getDrawable()).getBitmap();

                byte[] myNewImage = returnMyImage();

以下是我的DBHelper的相关内容:



    public static final String COLUMN_TITLE = "title";
    public static final String COLUMN_IMAGE = "image";
    public static final String COLUMN_ID = "id";

    public static final String CREATE_TABLE_ENTRY =
            "create table " + TABLE_SAVEIMAGE + "(" +
                    COLUMN_ID + " integer primary key autoincrement, " +
                    COLUMN_TITLE + " text not null, " +
                    COLUMN_IMAGE + " blob);";




这是我的DataSource的一部分:



    private String[] columns = {
            DBHelper.COLUMN_ID,
            DBHelper.COLUMN_TITLE,
            DBHelper.COLUMN_IMAGE,
    };

    public ImageEntry createEntry(String title, byte[] image){
        ContentValues values = new ContentValues();
        values.put(DBHelper.COLUMN_TITLE, title);
        values.put(DBHelper.COLUMN_IMAGE, image);
      }

    private ImageEntry populateEntry (Cursor cursor){
        int idIndex = cursor.getColumnIndex(DBHelper.COLUMN_ID);
        int titleIndex = cursor.getColumnIndex(DBHelper.COLUMN_TITLE);
        int imageIndex = cursor.getColumnIndex(DBHelper.COLUMN_IMAGE);

        ImageEntry imageentry = new ImageEntry(cursor.getString(titleIndex));
        foodentry.setId(cursor.getLong(idIndex));
        foodentry.setImage(cursor.getBlob(imageIndex));

        return imageentry;
    }




现在是我的ListFragment,我在那里拍摄了一张照片:



//For starting the camera in menu:
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.action_camera){
            startCamera();
        }
        return super.onOptionsItemSelected(item);
    }

    private void startCamera() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, CAMERA_REQUEST);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAMERA_REQUEST && resultCode == getActivity().RESULT_OK){
            ImageView imageView = (ImageView) getView().findViewById(R.id.imageView);
            Bitmap bitmap = (Bitmap) data.getExtras().get("data");
            bitmapGlobal = bitmap;
            imageView.setImageBitmap(bitmap);
        }
    }

//Here I click the button to add a new database entry:

buttonAdd.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                byte[] myNewImage = returnMyImage();
                String title = editTextAdd.getText().toString();
                dataSource.createEntry(title, myNewImage);
  
//And here is the method for "returnMyImage":
  
      public byte[] returnMyImage(){
        ImageView myImage1 = (ImageView) getView().findViewById(R.id.imageView);
        bitmapGlobal = ((BitmapDrawable) myImage1.getDrawable()).getBitmap();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmapGlobal.compress(Bitmap.CompressFormat.JPEG, 0, baos);
        return baos.toByteArray();
    }




我找不到问题。有什么想法吗?

编辑:这是完整的错误:

E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
        at com.example.sarah.saveyourImage.ImageEntryImage.returnMyImage(ImageEntryFragment.java:376)
        at com.example.sarah.saveyourImage.ImageEntryFragment$3.onClick(ImageEntryFragment.java:168)
        at android.view.View.performClick(View.java:4232)
        at android.view.View$PerformClick.run(View.java:17298)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4921)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
        at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案