Introduction
Many Android devices are equipped with built-in cameras. In this tutorial, we will work through the basic technique for capturing an image using the Android camera and then cropping it using apps the user already has installed on their device. Along the way, I’ll also show how to account for users whose devices do not support either the image capture or cropping actions.
Start New Project
Create a new Android project in Eclipse, or your chosen IDE. In your main Activity class, add the following import statements after the package declaration:
import
android.app.Activity;
import
android.content.ActivityNotFoundException;
import
android.content.Intent;
import
android.graphics.Bitmap;
import
android.net.Uri;
import
android.os.Bundle;
import
android.provider.MediaStore;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
import
android.widget.ImageView;
import
android.widget.Toast;
These will allow us to carry out image capture, crop and display within the app. Open your application’s strings XML file, which you should find in the “res/values” folder. Add the following strings:
<
string
name
=
"intro"
>Capture a picture to crop!</
string
>
<
string
name
=
"picture"
>Picture</
string
>
<
string
name
=
"capture"
>Launch Camera</
string
>
Implement the Layout for your App
Let’s design the app layout. Open your main XML file, which should be in the “res/layout” folder. Use a Linear Layout as follows, which Eclipse may have already provided:
android:layout_width
=
"fill_parent"
android:layout_height
=
"fill_parent"
android:orientation
=
"vertical"
>
</
LinearLayout
>
Inside the Linear Layout, add the following text display:
<
TextView
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:text
=
"@string/intro"
android:layout_margin
=
"3dp"
android:textStyle
=
"bold"
/>
Here we display some informative text using a string we defined in the strings XML file. After the Text View, add a button as follows:
<
Button
android:id
=
"@+id/capture_btn"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:text
=
"@string/capture"
/>
The ID value will allow us to identify the button in Java, so that we can respond to clicks. Again, we use a string we already defined in XML. Finally, after the button, add an Image View:
0 comments:
Post a Comment