Android 无法从SQLite数据库读取保存的图像

Android 无法从SQLite数据库读取保存的图像,android,sqlite,android-image,Android,Sqlite,Android Image,我在android应用程序中加入了“通过共享”选项。这是我第一次通过“共享”选项将照片添加到表中并成功查看。但下次我添加时,它会显示“已保存图像”,但无法使用显示功能查看(以前添加的照片显示正确)。我不知道是什么原因导致了这个问题,无论是在共享代码还是在数据库查看代码时出错。我已经附上了代码片段 Share.java String myPath = DB_PATH + DATABASE_NAME; //*receive image from 'share

我在android应用程序中加入了“通过共享”选项。这是我第一次通过“共享”选项将照片添加到表中并成功查看。但下次我添加时,它会显示“已保存图像”,但无法使用显示功能查看(以前添加的照片显示正确)。我不知道是什么原因导致了这个问题,无论是在共享代码还是在数据库查看代码时出错。我已经附上了代码片段

Share.java

   String myPath = DB_PATH + DATABASE_NAME; 
                //*receive image from 'share via' option
                // Get the intent that started this activity
               Intent intent = getIntent();
               Uri data = intent.getData();

                // Figure out what to do based on the intent type
               if (intent.getType().indexOf("image/") != -1) {
                 // Handle intents with image data ...
               try {
                    Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
                mBitmap =MediaStore.Images.Media.getBitmap(this.getContentResolver(),imageUri);
                // bimatp factory
                BitmapFactory.Options options = new BitmapFactory.Options();

                // downsizing image as it throws OutOfMemory Exception for larger
                // images
                options.inSampleSize = 2;

                ByteArrayOutputStream bos=new ByteArrayOutputStream();
                mBitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
                img =bos.toByteArray();
                if(img!=null){
                //open database in read and write mode
                checkDB = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READWRITE);
                if(checkDB!=null){

                ContentValues contentValues = new ContentValues();
                contentValues.put("myid", strEmailId);
                contentValues.put("image", img);
                database.insert("Images", null,contentValues);       
                displayToast("Image saved");

                }else 
                    displayToast("check is null");
               }else 
                    displayToast("img is null");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
               }
ShowPhotos.java

//adapter for gallery view
    ImageAdapter images = new ImageAdapter(this);
    final GridView gridView = (GridView) findViewById(R.id.gridview);
  //get the large image view
    int i=0;
    Cursor cursor = null;
    try {
      System.out.println("===================addImageToGallery===========================");
      db = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READONLY);
      cursor = db.rawQuery("SELECT _id, image FROM images WHERE myid='"+id+"'", null);
      if (cursor != null ) {
      strImageID =  new String[cursor.getCount()];
      strImagePath =  new String[cursor.getCount()];
        if  (cursor.moveToFirst()) {
        do{
              strImageID[i] = cursor.getString(cursor.getColumnIndex("_id"));
              byte[] data = cursor.getBlob(cursor.getColumnIndex("image"));
              //Bitmap thumbnail = BitmapFactory.decodeByteArray(data, 0, data.length);
              //images.AddImage(thumbnail);
              images.AddImage(ShrinkBitmap(data, getWindowManager().getDefaultDisplay().getWidth(), getWindowManager().getDefaultDisplay().getHeight()));
              gridView.setAdapter(images);
              i++;
        }while(cursor.moveToNext());
        }
        }
      } catch (Exception e){
      System.out.println("addImageToGallery() : "+e.toString());
      }finally{
      if(cursor != null)cursor.close();
      }

请帮我找出错误。

我在代码中犯的错误是,在将字节流保存到数据库之前,我忘记了检查字节流的值(无论它是否为null)。当遇到空值且之后输入的值未被读取时,show函数始终停止读取