Question

I have included 'share via' option in my android app. Very first time when I add a photo by share via option its added to my table and viewed successfully. But next time when I add it shows 'image saved' but couldn't see using show function(Previously added photos showed correctly). I don't know what causes the problem whether error is in sharing code or database viewing code. I have attached the code snippets.

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

Please help me to find out the bug..

Was it helpful?

Solution

The mistake I have done in the code is that I forget to check the value of byte stream before saving it in database(whether it is null or not). show function always stops reading when a null value encountered and values entered afterwards were not read.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top