Showing posts with label intent. Show all posts
Showing posts with label intent. Show all posts

Saturday, 26 April 2014


email using intent
Smartphones allows to access to lots of different activities as well as sending text and calling some one. These include the ability to surf the web from your handset and even sync your device with an email account to read and send messages from your mobile phone. Smartphones running Google Android are no exception. You can easily configure your smartphone to access your primary email account with commonly built in Gmail app in all the smartphones.
     So today I am going to show you how to send email from your application using intent to send it using gmail. 

Some Advantages of Email:

  • Emails are easy to use. You can organize your daily correspondence, send and receive electronic messages.
  • Emails are fast. They are delivered at once around the world. No other form of written communication is as fast as an email.
  • The language used in emails is simple and informal.
  • When you reply to an email you can attach the original message so that when you answer the recipient knows what you are talking about. This is important if you get hundreds of emails a day.
  • It is possible to send automated emails with a certain text. In such a way it is possible to tell the sender that you are on vacation. These emails are called auto responders.
  • Emails do not use paper. They are environment friendly and save a lot of trees from being cut down.
  • Emails can also have pictures in them. You can send birthday cards or newsletters as emails.
  • Products can be advertised with emails. Companies can reach a lot of people and inform them in a short time.

OutPut:

emailOnlaunchemail Text and Image

email attach imageemail selectemail send using gmail
Create new Android Project
Project Name: SendEmail
//tested from 2.3.3 to current android sdk 
Build Target: Android 2.3.3   //or greater than that
Application Name: SendEmail
Package Name: com.shaikhhamadali.blogspot.sendemail
Create Layout file: activity_send_email


1. code of Layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <!-- set input type email to launch soft keyboard containing .com and @ sign -->

    <EditText
        android:id="@+id/edTMail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="email address"
        android:inputType="textEmailAddress" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/edTSubject"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="subject" />

    <EditText
        android:id="@+id/edTMessage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="message" />

    <EditText
        android:id="@+id/edTSignature"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="signature" />

    <ImageButton
        android:id="@+id/imBtnImage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:maxHeight="150dp"
        android:maxWidth="150dp"
        android:src="@drawable/attachment" />

    <Button
        android:id="@+id/btnSendMail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="send email" />

</LinearLayout>

2. code of activity:


package com.shaikhhamadali.blogspot.sendemail;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

public class SendEmail extends Activity {
 //Create variables
 Uri selectedImageUri;
 //declare views/controls
 Button btnSendMail;
 ImageButton imBtnImage;
 EditText edTMail,edTSubject,edTMessage,edTSignature;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_send_email);
  initializeControls();
 }
 private void initializeControls() {
  //get reference of the UI Controls
  edTMail=(EditText)findViewById(R.id.edTMail);
  edTSubject=(EditText)findViewById(R.id.edTSubject);
  edTMessage=(EditText)findViewById(R.id.edTMessage);
  edTSignature=(EditText)findViewById(R.id.edTSignature);
  imBtnImage=(ImageButton)findViewById(R.id.imBtnImage);
  btnSendMail =(Button)findViewById(R.id.btnSendMail);
  //Set click listeners to Buttons
  imBtnImage.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    // Create instance of intent
    Intent intent = new Intent();
    // set type as image
    intent.setType("image/*");
    /*set action as ACTION_GET_CONTENT(Activity Action:
                Allow the user to select a particular kind of data and return it.)*/
    intent.setAction(Intent.ACTION_GET_CONTENT);
    /*start activity for result and pass create Chooser 
                 and set title of dialog as Select Picture*/
    startActivityForResult(Intent.createChooser(intent,
      "Select Picture"), 1);
   }
  });
  btnSendMail.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {

    // Create instance of intent
    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[]{edTMail.getText().toString()});
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, edTSubject.getText().toString());
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, edTMessage.getText().toString()+"\n\n"+edTSignature.getText().toString()); 
    // check that is user picked an image or not
    if(selectedImageUri!=null){
     emailIntent.setType("image/jpeg");
     //attach image to email by telling intent the uri of image
     emailIntent.putExtra(Intent.EXTRA_STREAM, selectedImageUri);
    }
    else emailIntent.setType("text/plain");
    /*start activity and pass create Chooser 
                  and set title of dialog as Send your email in: */
    startActivity(Intent.createChooser(emailIntent, "Send your email in:"));
   }
  });
 }
 public void onActivityResult(int requestCode, int resultCode, Intent data) {
  //RESULT_OK means that user has selected an image 
  if (resultCode == RESULT_OK) {
   //check that is the same request code that pass above 
   if (requestCode == 1) {
    //get uri from data for attachment as image
    selectedImageUri = data.getData();
    //set image on imagebutton to show the selection
    imBtnImage.setImageURI(selectedImageUri);
   }
  }
 }

}

3. note that:

4. conclusion:

  • Some information about how to email using intent.
  • Some information about how to pick image using intent.
  • know how to attach email and add signature using intent.
  • Know how ACTION_SEND help to email text and attach image as well.
  • know what are some of the advantages of using email.

    5. About the post:

    • The code seems to explain itself due to comments, but if you have any questions you can freely ask too!
    •  Don’t mind to write a comment whatever you like to ask, to know,to suggest or recommend.
    •  Hope you enjoy it!
        6. Source Code:
                you can download the source code from: GoogleDriveGithub

        Cheers,
        Hamad Ali Shaikh

        Sunday, 29 September 2013

        Camera lauch on button click(Capture/Crop App)

        Capture Images and Crop Images using Intent

        Crop Captured image(Capture/Crop App)

        Capture Images and Crop Images using Intent

        Show Croped image(Capture/Crop App)

        Capture Images and Crop Images using Intent

        Create new Android Project
        Project Name: Take picture from Camera
        //tested from 2.3.3 to current android sdk 
        Build Target: Android 2.3.3   //or greater than that
        Application Name: Take_Pic_Camera
        Package Name: com.shaikhhamadali.blogspot.take_pic_camera
        Create layout file: activity_launch_camera 

          1. create layout: 
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".LaunchCamera"
            android:background="#000000" >
        
            <ImageView
                android:id="@+id/imVCature_pic"
                android:layout_width="match_parent"
                android:layout_height="350dp"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="50dp"
                android:contentDescription="catured picture"
                android:src="@drawable/ic_launcher" />
        
            <Button
                android:id="@+id/btnCapture"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="400dp"
                android:text="Take Picture" />
        
        </RelativeLayout>

        2.code of activity:


        package com.shaikhhamadali.blogspot.take_pic_camera;
        
        import java.io.File;
        
        import android.net.Uri;
        import android.os.Bundle;
        import android.os.Environment;
        import android.provider.MediaStore;
        import android.app.Activity;
        import android.content.ActivityNotFoundException;
        import android.content.Intent;
        import android.graphics.Bitmap;
        import android.graphics.BitmapFactory;
        import android.view.View;
        import android.view.View.OnClickListener;
        import android.widget.Button;
        import android.widget.ImageView;
        import android.widget.Toast;
        
        public class LaunchCamera extends Activity {
         ImageView imVCature_pic;
         Button btnCapture;
        
         @Override
         protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_launch_camera);
          initializeControls();
         }
        
         private void initializeControls() {
          imVCature_pic=(ImageView)findViewById(R.id.imVCature_pic);
          btnCapture=(Button)findViewById(R.id.btnCapture);
          btnCapture.setOnClickListener(new OnClickListener() {
        
           @Override
           public void onClick(View v) {
            /* create an instance of intent
             * pass action android.media.action.IMAGE_CAPTURE 
             * as argument to launch camera
             */
            Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
            /*create instance of File with name img.jpg*/
            File file = new File(Environment.getExternalStorageDirectory()+File.separator + "img.jpg");
            /*put uri as extra in intent object*/
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
            /*start activity for result pass intent as argument and request code */
            startActivityForResult(intent, 1);
           }
          });
        
         }
        
         @Override
         protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
          //if request code is same we pass as argument in startActivityForResult
          if(requestCode==1){
           //create instance of File with same name we created before to get image from storage
           File file = new File(Environment.getExternalStorageDirectory()+File.separator + "img.jpg");
           //Crop the captured image using an other intent
           try {
            /*the user's device may not support cropping*/
            cropCapturedImage(Uri.fromFile(file));
           }
           catch(ActivityNotFoundException aNFE){
            //display an error message if user device doesn't support
            String errorMessage = "Sorry - your device doesn't support the crop action!";
            Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
            toast.show();
           }
          }
          if(requestCode==2){
           //Create an instance of bundle and get the returned data
           Bundle extras = data.getExtras();
           //get the cropped bitmap from extras
           Bitmap thePic = extras.getParcelable("data");
           //set image bitmap to image view
           imVCature_pic.setImageBitmap(thePic);
          }
         }
         //create helping method cropCapturedImage(Uri picUri)
         public void cropCapturedImage(Uri picUri){
          //call the standard crop action intent 
          Intent cropIntent = new Intent("com.android.camera.action.CROP");
          //indicate image type and Uri of image
          cropIntent.setDataAndType(picUri, "image/*");
          //set crop properties
          cropIntent.putExtra("crop", "true");
          //indicate aspect of desired crop
          cropIntent.putExtra("aspectX", 1);
          cropIntent.putExtra("aspectY", 1);
          //indicate output X and Y
          cropIntent.putExtra("outputX", 256);
          cropIntent.putExtra("outputY", 256);
          //retrieve data on return
          cropIntent.putExtra("return-data", true);
          //start the activity - we handle returning in onActivityResult
          startActivityForResult(cropIntent, 2);
         }
        }

        3. note that:

        • you can use this on button onclick,on action_down,on the fly etc.
        • you can capture image by multiple ways one of them i have described above.
        • you can crop image by multiple ways too but this one is using intent.
        • about Camera.

        4. conclusion:

        • Some information about how to launch camera using intent and crop image using intent too.
        • Know how to decode image from storage to bitmap from my previous post.
        • know what is Bitmap factory options and how to use them from my previous post.

        5. about the post:

        • The code seems to explain itself due to comments, but if you have any questions you can freely ask too!
        •  Don’t mind to write a comment whatever you like to ask, to know,to suggest or recommend.
        •  Hope you enjoy it!
        6. Source Code:
                you can download the source code here

        Cheers,
        Hamad Ali Shaikh

        Saturday, 28 September 2013

        Camera lauch on button click

        Capture Images using Intent (Android)

        Show Captured image

        Capture Images using Intent (Android)

        Create new Android Project
        Project Name: Take picture from Camera
        //tested from 2.3.3 to current android sdk 
        Build Target: Android 2.3.3   //or greater than that
        Application Name: Take_Pic_Camera
        Package Name: com.shaikhhamadali.blogspot.take_pic_camera
        Create layout file: activity_launch_camera 



          1. create layout: 

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".LaunchCamera"
            android:background="#000000" >
        
            <ImageView
                android:id="@+id/imVCature_pic"
                android:layout_width="match_parent"
                android:layout_height="350dp"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="50dp"
                android:contentDescription="catured picture"
                android:src="@drawable/ic_launcher" />
        
            <Button
                android:id="@+id/btnCapture"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="400dp"
                android:text="Take Picture" />
        
        </RelativeLayout>

        2.code od activity:


        package com.shaikhhamadali.blogspot.take_pic_camera;
        
        import java.io.File;
        
        import android.net.Uri;
        import android.os.Bundle;
        import android.os.Environment;
        import android.provider.MediaStore;
        import android.app.Activity;
        import android.content.Intent;
        import android.graphics.Bitmap;
        import android.graphics.BitmapFactory;
        import android.view.View;
        import android.view.View.OnClickListener;
        import android.widget.Button;
        import android.widget.ImageView;
        
        public class LaunchCamera extends Activity {
         ImageView imVCature_pic;
         Button btnCapture;
        
         @Override
         protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_launch_camera);
          initializeControls();
         }
        
         private void initializeControls() {
          imVCature_pic=(ImageView)findViewById(R.id.imVCature_pic);
          btnCapture=(Button)findViewById(R.id.btnCapture);
          btnCapture.setOnClickListener(new OnClickListener() {
        
           @Override
           public void onClick(View v) {
            /* create an instance of intent
             * pass action android.media.action.IMAGE_CAPTURE 
             * as argument to launch camera
             */
            Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
            /*create instance of File with name img.jpg*/
            File file = new File(Environment.getExternalStorageDirectory()+File.separator + "img.jpg");
            /*put uri as extra in intent object*/
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
            /*start activity for result pass intent as argument and request code */
            startActivityForResult(intent, 1);
           }
          });
        
         }
         
         @Override
         protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
          //if request code is same we pass as argument in startActivityForResult
          if(requestCode==1){
           //create instance of File with same name we created before to get image from storage
           File file = new File(Environment.getExternalStorageDirectory()+File.separator + "img.jpg");
           //get bitmap from path with size of
           imVCature_pic.setImageBitmap(decodeSampledBitmapFromFile(file.getAbsolutePath(), 600, 450));
           }
         }
         public static Bitmap decodeSampledBitmapFromFile(String path,
           int reqWidth, int reqHeight) { 
          // First decode with inJustDecodeBounds=true to check dimensions
          final BitmapFactory.Options options = new BitmapFactory.Options();
          //Query bitmap without allocating memory
          options.inJustDecodeBounds = true;
          //decode file from path
          BitmapFactory.decodeFile(path, options);
          // Calculate inSampleSize
          // Raw height and width of image
          final int height = options.outHeight;
          final int width = options.outWidth;
          //decode according to configuration or according best match
          options.inPreferredConfig = Bitmap.Config.RGB_565;
          int inSampleSize = 1;
          if (height > reqHeight) {
           inSampleSize = Math.round((float)height / (float)reqHeight);
          }
          int expectedWidth = width / inSampleSize;
          if (expectedWidth > reqWidth) {
           //if(Math.round((float)width / (float)reqWidth) > inSampleSize) // If bigger SampSize..
           inSampleSize = Math.round((float)width / (float)reqWidth);
          }
          //if value is greater than 1,sub sample the original image
          options.inSampleSize = inSampleSize;
          // Decode bitmap with inSampleSize set
          options.inJustDecodeBounds = false;
          return BitmapFactory.decodeFile(path, options);
         }
        }

        3. note that:

        • you can use this on button onclick,on action_down,on the fly etc.
        • you can capture image by multiple ways one of them i have described above.

        4. conclusion:

        • Some information about how to launch camera using intent.
        • Know how to decode from storage to bitmap.
        • know what is Bitmap factory options and how to use them.

        5. About the post:

        • The code seems to explain itself due to comments, but if you have any questions you can freely ask too!
        •  Don’t mind to write a comment whatever you like to ask, to know,to suggest or recommend.
        •  Hope you enjoy it!
        6. Source Code:
                you can download the source code here

        Cheers,
        Hamad Ali Shaikh