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

时间:2013-12-30 02:27:04

标签: android sqlite android-image

我在我的Android应用中包含了'share via'选项。第一次当我通过分享添加照片时,将其添加到我的桌面并成功查看。但是下次我添加它时会显示“图像已保存”,但无法看到使用show功能(之前添加的照片显示正确)。我不知道是什么原因导致问题是共享代码还是数据库查看代码中的错误。我附上了代码片段。

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

请帮我找出错误..

1 个答案:

答案 0 :(得分:0)

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