compress and decode image and save in sqlite
<p>i follow a code which compress and decode image and save in sql lite but is used gellery or camera i want to decode and compress image from drawable folder what do i do? i paste below both code   what i write in filepath instead of     final String imageFilePath = cursor.getString(0);??</p>

<p>below is my code and  a code which i follow</p>

<pre><code>   rrayList&lt;Contact&gt; imageArry = new ArrayList&lt;Contact&gt;();
      ContactImageAdapter adapter;
     Button BrowseButton;
     Button BrowseButton2;
    DataBaseHandler db;
    public  static Bitmap image ;
   public  static byte[] imageInByte;




       ByteArrayOutputStream stream = new ByteArrayOutputStream();
    /** Called when the activity is first created. */
    @Override
   public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

       db = new DataBaseHandler(this);



         BrowseButton=(Button)findViewById(R.id.BrowseButton);
     BrowseButton2=(Button)findViewById(R.id.BrowseButton2);

   adapter = new ContactImageAdapter(this, R.layout.screen_list,    imageArry);

     BrowseButton.setOnClickListener(new View.OnClickListener() {

   @Override
   public void onClick(View v) {



    image.compress(CompressFormat.JPEG, 100, stream);
     imageInByte = stream.toByteArray();

    Log.d("Insert: ", "Inserting ..");
    db.addContact(new Contact("FaceBook", imageInByte));



             }
    });

      BrowseButton2.setOnClickListener(new View.OnClickListener() {

   @Override
    public void onClick(View v) {


    List&lt;Contact&gt; contacts = db.getAllContacts();
    for (Contact cn : contacts) {




    String log = "ID:" + cn.getID() + " Name: " + cn.getName()      + " ,Image: " +  
     cn.getImage();


    //Writing Contacts to log
    Log.d("Result: ", log);
    //add contacts data in arrayList
    imageArry.add(cn);

    }

    ListView dataList = (ListView) findViewById(R.id.list);
    dataList.setAdapter(adapter);

    try {
        stream.close();
        stream = null;
    } catch (IOException e) {

        e.printStackTrace();
    }

 }
 });






   final String imageFilePath = cursor.getString(0);
    File photos = new File(imageFilePath);
      image = decodeFile(photos);






   }



























      private Bitmap decodeFile(File f) {
       try {
    // decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(new FileInputStream(f), null, o);

    // Find the correct scale value. It should be the power of 2.
    final int REQUIRED_SIZE = 70;
    int width_tmp = o.outWidth, height_tmp = o.outHeight;
    int scale = 1;
    while (true) {
        if (width_tmp / 2 &lt; REQUIRED_SIZE || height_tmp / 2 &lt; REQUIRED_SIZE)
            break;
        width_tmp /= 2;
        height_tmp /= 2;
        scale++;
    }

    // decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
}
catch (FileNotFoundException e) {
}
return null;
    }

      }










       &lt;---------------    code that i follow is here ----------&gt;


















                   package com.example.worldcountriesbooks;




      public class AddEditVisit extends Activity {

 private long rowID; 
 private EditText Address;
 private EditText Stage;
 private EditText Dueby;

 private EditText Donedate;
 private EditText Notes;
 private EditText Person;
 private  ImageView imageView1;
public  static Bitmap yourSelectedImage = null;
public static byte[] blob = null;



   private final int CAMERA_PICTURE = 1;
   private final int GALLERY_PICTURE = 2;
   private Intent pictureActionIntent = null;
   public final String SDCARD_ROOT_PATH =    
     Environment.getExternalStorageDirectory().getAbsolutePath();
public final String SAVE_PATH_IN_SDCARD = "/myFolder23/"; 
public final String IMAGE_CAPTURE_NAME    
      ="imgtemp"+System.currentTimeMillis()+".jpeg"; 

//public static        byte[] blob = new byte[2048];






   @Override
   public void onCreate(Bundle savedInstanceState) 
   {
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.add_country);

      Address = (EditText) findViewById(R.id.Address);
      Stage = (EditText) findViewById(R.id.Stage);
      Dueby = (EditText) findViewById(R.id.Dueby);

      Donedate = (EditText) findViewById(R.id.Donedate);

      Notes = (EditText) findViewById(R.id.Notes);
      Person = (EditText) findViewById(R.id.Person);

      imageView1 = (ImageView) findViewById(R.id.imageView1);
      Button Browse = (Button) findViewById(R.id.Browse);






      Browse.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            {               
        //      Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        //      intent.setType("image/*");
         //     startActivityForResult(intent, 0);

                startDialog();
            }
        });        






      Bundle extras = getIntent().getExtras(); 

      if (extras != null)
      {
         rowID = extras.getLong("row_id");
         Address.setText(extras.getString("Address"));  
         Stage.setText(extras.getString("Stage"));  
         Dueby.setText(extras.getString("Dueby"));  
         Donedate.setText(extras.getString("Donedate"));  
         Notes.setText(extras.getString("Notes")); 
         Person.setText(extras.getString("Person")); 



      }







      Button saveButton =(Button) findViewById(R.id.saveBtn);
      saveButton.setOnClickListener(new OnClickListener() {

          public void onClick(View v) 
          {
             if (Address.getText().length() != 0)
             {
                AsyncTask&lt;Object, Object, Object&gt; saveContactTask = 
                   new AsyncTask&lt;Object, Object, Object&gt;() 
                   {
                      @Override
                      protected Object doInBackground(Object... params) 
                      {
                         saveContact();
                         return null;
                      }

                      @Override
                      protected void onPostExecute(Object result) 
                      {
                         finish();
                      }
                   }; 

                saveContactTask.execute((Object[]) null); 
             }

             else
             {
                AlertDialog.Builder alert = new  
       AlertDialog.Builder(AddEditVisit.this);
                alert.setTitle(R.string.errorTitle); 
                alert.setMessage(R.string.errorMessage);
                alert.setPositiveButton(R.string.errorButton, null); 
                alert.show();
             }
          } 
     });
   }



   private void saveContact() 
   {


//     byte[] blob = new byte[2048];

       ByteArrayOutputStream outStr = new ByteArrayOutputStream();
       yourSelectedImage.compress(CompressFormat.JPEG, 100, outStr);
        blob = outStr.toByteArray();





      DatabaseConnector dbConnector = new DatabaseConnector(this);

      if (getIntent().getExtras() == null)
      {
          dbConnector.insertContact(Address.getText().toString(),
                  Stage.getText().toString(),
                  Dueby.getText().toString(),
                  Donedate.getText().toString(),
                  Notes.getText().toString(),
                  Person.getText().toString()
                  ,blob




                  );
      }
      else
      {
         dbConnector.updateContact(rowID,
                 Address.getText().toString(),
                 Stage.getText().toString(), 
                 Dueby.getText().toString(), 
            Donedate.getText().toString(),
         Notes.getText().toString(),
          Person.getText().toString()
          , blob


         );
      }
   }










   @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent  
      imageReturnedIntent) {
       super.onActivityResult(requestCode, resultCode, imageReturnedIntent);



       if (requestCode == GALLERY_PICTURE) {
            Uri uri = imageReturnedIntent.getData();
            if (uri != null) {
                // User had pick an image.
                Cursor cursor = getContentResolver().query(uri, new String[]   
 {   android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
                cursor.moveToFirst();
                final String imageFilePath = cursor.getString(0);
                File photos = new File(imageFilePath);
                yourSelectedImage = decodeFile(photos);
                yourSelectedImage =  
    Bitmap.createScaledBitmap(yourSelectedImage, 150, 150, true);
                imageView1.setImageBitmap(yourSelectedImage);
                cursor.close();
            }
            else {
                Toast toast = Toast.makeText(this, "No Image is selected.",  
   Toast.LENGTH_LONG);
                toast.show();
            }
        }
        else if (requestCode == CAMERA_PICTURE) {
            if (imageReturnedIntent.getExtras() != null) {
                // here is the image from camera
                yourSelectedImage = (Bitmap)  
    imageReturnedIntent.getExtras().get("data");
                imageView1.setImageBitmap(yourSelectedImage);
                deleteLastCapturedImage();
            }
        }




    }


   private Bitmap decodeFile(File f) {
        try {
            // decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f), null, o);

            // Find the correct scale value. It should be the power of 2.
            final int REQUIRED_SIZE = 70;
            int width_tmp = o.outWidth, height_tmp = o.outHeight;
            int scale = 1;
            while (true) {
                if (width_tmp / 2 &lt; REQUIRED_SIZE || height_tmp / 2 &lt;  
     REQUIRED_SIZE)
                    break;
                width_tmp /= 2;
                height_tmp /= 2;
                scale++;
            }

            // decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), null,  
    o2);
        }
        catch (FileNotFoundException e) {
        }
        return null;
    }

    private void startDialog() {
        AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this);
        myAlertDialog.setTitle("Upload Pictures Option");
        myAlertDialog.setMessage("How do you want to set your picture?");

        myAlertDialog.setPositiveButton("Gallery", new   
   DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
                pictureActionIntent = new Intent(Intent.ACTION_GET_CONTENT,  
    null);
                pictureActionIntent.setType("image/*");
                pictureActionIntent.putExtra("return-data", true);
                startActivityForResult(pictureActionIntent, GALLERY_PICTURE);
            }
        });

        myAlertDialog.setNegativeButton("Camera", new  
  DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
                pictureActionIntent = new  
   Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);











                File dir = new File(Environment.getExternalStorageDirectory()  
   + "/myFolder23");
                if(dir.exists() &amp;&amp; dir.isDirectory()) {
                  // do something here
                 }
                else{
                    //create dir here
                    dir.mkdir(); 
                   }
                 Intent pictureActionIntent = new  
  Intent(MediaStore.ACTION_IMAGE_CAPTURE);  


                 pictureActionIntent.putExtra(MediaStore.EXTRA_OUTPUT,   
  Uri.fromFile(new
                         File(SDCARD_ROOT_PATH +  
   SAVE_PATH_IN_SDCARD,IMAGE_CAPTURE_NAME)));  




                  startActivityForResult(pictureActionIntent,CAMERA_PICTURE);  




            }
        });
        myAlertDialog.show();
    }



    public void deleteLastCapturedImage() {
        String[] projection = { 
                MediaStore.Images.ImageColumns.SIZE,
                MediaStore.Images.ImageColumns.DISPLAY_NAME,
                MediaStore.Images.ImageColumns.DATA, 
                BaseColumns._ID
        };

        Cursor c = null;
        Uri u = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

        try {
            if (u != null) {
                c = managedQuery(u, projection, null, null, null);
            }
            if ((c != null) &amp;&amp; (c.moveToLast())) {

                ContentResolver cr = getContentResolver();
                int i =  
  cr.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, BaseColumns._ID + "=" +  
  c.getString(c.getColumnIndex(BaseColumns._ID)), null);

           //     Log.v(LOG_TAG, "Number of column deleted : " + i);

            }
        } finally {
            if (c != null) {
                c.close();
            }
        }
    }

}
</code></pre>
