.
109-Programmr: Android Camera
Camera is one of the most important part of mobile apps. It allows the user to save personal profile photo or send attachment to accompany their text messages.
|
0) Starting Up
Continue from the previous tutorial, http://android-steps.blogspot.my/2015/01/108-programmr-android-storage-shared.html or download startup files here.
1) Adding OnClick Event and Responding Action
Edit the XML file (activity_settings.xml) to add onclick event to ImageView and assign it to setPhoto method.
Edit the Java file (Settings.java) to add setPhoto method.
|
File: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
>
<ImageView
android:id="@+id/ivw_user"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="15dp"
android:src="@drawable/smiley"
android:layout_gravity="center_horizontal"
android:onClick="setPhoto"
/>
<EditText
android:id="@+id/etx_user"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
/>
<EditText
android:id="@+id/etx_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
/>
<Button
android:id="@+id/btn_save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Save"
android:onClick="saveSettings"
/>
</LinearLayout>
|
File: Settings.java
package com.example.MyAndroid;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.widget.EditText;
import android.content.SharedPreferences;
public class Settings extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
loadSettings();
}
public void saveSettings(View v){
Toast.makeText(getApplicationContext(), "Saving Data...", Toast.LENGTH_LONG).show();
SharedPreferences preferences = getSharedPreferences("myprefs", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
EditText etxUser=(EditText) findViewById(R.id.etx_user);
EditText etxEmail=(EditText) findViewById(R.id.etx_email);
editor.putString("name",etxUser.getText().toString());
editor.putString("email",etxEmail.getText().toString());
editor.commit();
finish();
}
private void loadSettings(){
Toast.makeText(getApplicationContext(), "Loading Data...", Toast.LENGTH_LONG).show();
SharedPreferences preferences = getSharedPreferences("myprefs", MODE_PRIVATE);
EditText etxUser=(EditText) findViewById(R.id.etx_user);
EditText etxEmail=(EditText) findViewById(R.id.etx_email);
// If value for key not exist then
// return second param value - In this case "..."
etxUser.setText(preferences.getString("name", "..."));
etxEmail.setText(preferences.getString("email", "..."));
}
public void setPhoto(View v){
Toast.makeText(getApplicationContext(), "Activating Camera...", Toast.LENGTH_LONG).show();
}
}
|
OUTCOME.
2) Adding Intent and StartActivityForResult
We use Intent to activate the camera and StartActivityForResult method to obtain image from the camera.
Once the image is available, we assign the image to the ImageView object.
Update the AndroidManifest to allow the apps to use the Camera Hardware.
|
File: Settings.java
package com.example.MyAndroid;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.widget.EditText;
import android.content.SharedPreferences;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.ImageView;
public class Settings extends Activity
{
private static final int IMAGE_CAPTURE = 1; //identifier for startActivityForResult
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
loadSettings();
}
public void saveSettings(View v){
Toast.makeText(getApplicationContext(), "Saving Data...", Toast.LENGTH_LONG).show();
SharedPreferences preferences = getSharedPreferences("myprefs", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
EditText etxUser=(EditText) findViewById(R.id.etx_user);
EditText etxEmail=(EditText) findViewById(R.id.etx_email);
editor.putString("name",etxUser.getText().toString());
editor.putString("email",etxEmail.getText().toString());
editor.commit();
finish();
}
private void loadSettings(){
Toast.makeText(getApplicationContext(), "Loading Data...", Toast.LENGTH_LONG).show();
SharedPreferences preferences = getSharedPreferences("myprefs", MODE_PRIVATE);
EditText etxUser=(EditText) findViewById(R.id.etx_user);
EditText etxEmail=(EditText) findViewById(R.id.etx_email);
// If value for key not exist then
// return second param value - In this case "..."
etxUser.setText(preferences.getString("name", "..."));
etxEmail.setText(preferences.getString("email", "..."));
}
public void setPhoto(View v){
Toast.makeText(getApplicationContext(), "Activating Camera...", Toast.LENGTH_LONG).show();
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, IMAGE_CAPTURE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case IMAGE_CAPTURE: this.imageFromCamera(resultCode, data);
break;
default:
break; }
}
}
private void imageFromCamera(int resultCode, Intent data) {
ImageView ivwUser=(ImageView) findViewById(R.id.ivw_user);
ivwUser.setImageBitmap((Bitmap) data.getExtras().get("data"));
}
}
|
File: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.MyAndroid"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.CAMERA" />
<application android:label="MyAndroid" android:icon="@drawable/ic_launcher">
<activity android:name="MyAndroid"
android:label="MyAndroid">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="About"
android:label="About">
</activity>
<activity android:name="Settings"
android:label="Settings">
</activity>
</application>
<uses-sdk android:minSdkVersion="8" />
</manifest>
|
OUTCOME.
We need to use a real device to test the camera.
Bear in mind that at this stage, the captured image is not saved to the apps memory. The next time you come to this page, the image reverts back to Smiley.
We need to add additional codes to the saveSettings and loadSettings method in order to keep the image in the apps memory.
Follow the intermediate tutorial to add Gallery and image storage to this app,
and
|
DOWNLOAD
FURTHER READING
.
No comments:
Post a Comment