Friday, 3 May 2013

Get images from Gallery and Camera and Mail with attachment in Android


final int RQS_GALARRY = 0;
final int RQS_CAMERA = 2;

Uri imageUri = null;
Bitmap bmp;
ImageView YD_image;


btnGal.setOnClickListener(new OnClickListener()
{                 
       @Override
       public void onClick(View v)
       {
              Intent intent = new Intent(Intent.ACTION_PICK,
                           android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
              startActivityForResult(intent, RQS_GALARRY);

       }
});
btnCamera.setOnClickListener(new OnClickListener()
{                 
       @Override
       public void onClick(View v)
       {
              String fileName = "temp.png";
              ContentValues values = new ContentValues();
              values.put(MediaStore.Images.Media.TITLE, fileName);
              imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); 

              Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
              intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
              startActivityForResult(intent, RQS_CAMERA);

       }
});


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{

       super.onActivityResult(requestCode, resultCode, data);

       if (resultCode == RESULT_OK)
       {
              switch(requestCode)
              {
              case RQS_GALARRY:

                     try
                     {

                           System.gc();
                           imageUri = data.getData();
                           String selectedImagePath = getPath(imageUri);
                           if(bmp != null && !bmp.isRecycled())
                           {
                                  bmp = null
                                  ((BitmapDrawable)YD_image.getDrawable()).getBitmap().recycle();
                           } 

                           bmp = BitmapFactory.decodeFile(selectedImagePath);
                           YD_image.setBackgroundResource(0);
                           YD_image.setImageBitmap(bmp);

                           mpopup.dismiss();   

                           dialog_message("Prescription uploaded");
                     }
                     catch (Exception e)
                     {
                           e.printStackTrace();
                     }


                     break;

              case RQS_CAMERA:

                     try
                     {
                           if(bmp != null && !bmp.isRecycled())
                           {
                                  bmp = null;             
                           }
                           bmp = (Bitmap) data.getExtras().get("data");
                           YD_image.setImageBitmap(bmp);   
                           mpopup.dismiss();

                           dialog_message("Prescription uploaded");


                     }
                     catch (Exception e)
                     {
                           e.printStackTrace();
                     }

                     break;

              }

       }
}

public String getPath(Uri uri)
{
       String[] projection = { MediaStore.Images.Media.DATA };
       Cursor cursor = managedQuery(uri, projection, nullnullnull);
       int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
       cursor.moveToFirst();
       return cursor.getString(column_index);
}
private void SendMail()

       String body ="Hasmukh Bhadani":

              final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
       emailIntent.putExtra(Intent.EXTRA_EMAIL,new String[]{Toemail});
       emailIntent.setType("text/html");
       emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject");
       emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml(body));
       try
       {  
              if(imageUri != null)
              {
                     emailIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
                     emailIntent.setType("image/png");
              }
              else
              {
                     emailIntent.setType("plain/text");
              }
              startActivity(Intent.createChooser(emailIntent,"Send Mail........"));


       }
       catch (android.content.ActivityNotFoundException ex)
       {
              Toast.makeText(Act_YourDetails.this"There are no email clients installed.", Toast.LENGTH_SHORT).show();
       }

}


I will be happy if you will provide your feedback or follow this blog. Any suggestion and help will be appreciated.
Thank you :)