Вы находитесь на странице: 1из 11

Android Camera

Example

Intents:
An Intent is exactly what it describes. It's an

"intention" to do an action.
An Intent is basically a message to say you
did or want something to happen. Depending
on the intent, apps or the OS might be
listening for it and will react accordingly.
Intents can be used to signal to the Android
system that a certain event has occurred.
Other components in Android can register to
this event via an intent filter.

Manifest Declarations

Before starting development on your

application with the Camera , you should


make sure your manifest has the appropriate
declarations to allow use of camera hardware
and other related features.
<uses-permission

android:name="android.permission.CAMERA" />
<uses-feature
android:name="android.hardware.camera" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STOR
AGE" />

ACTION_IMAGE_CAPTURE
Standard Intent action that can be sent to

have the camera application capture an image


and return it.

Example:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="grewe.example.Camera.Simple"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="12" />
<uses-permission android:name="android.permission.CAMERA"></uses-permission>
<uses-feature android:name="android.hardware.camera" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".CameraSimpleActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

import
import
import
import
import
import
import
import
import
import
import
import
import
import
import

android.os.Bundle;
android.view.Menu;
android.view.MenuItem;
android.view.View;
android.widget.TextView;
android.annotation.SuppressLint;
android.content.Intent;
android.provider.MediaStore;
android.widget.Toast;
android.net.Uri;
java.io.File;
android.os.Environment;
android.app.Activity;
android.widget.ImageView;
android.graphics.Bitmap;

public class CameraSimpleActivity extends Activity {

private static final int ACTIVITY_START_CAMERA_APP=0;


private ImageView mphotocaptureimageview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mphotocaptureimageview=(ImageView) findViewById (R.id.capture);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so
long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

public void photo(View view)


{
Intent icamera= new Intent();
icamera.setAction(MediaStore.ACTION_IMAGE_CAPTURE)
;
startActivityForResult(icamera,ACTIVITY_START_CAMERA
_APP );
}

protected void OnActivityResult(int requestCode,int


resultCode,Intent data)
{
if(requestCode== ACTIVITY_START_CAMERA_APP &&
resultCode==RESULT_OK)
{
//Toast.makeText(this,"it works",Toast.LENGTH_SHORT).show();
Bundle extras=data.getExtras();
Bitmap photocapturebitmap= (Bitmap) extras .get("data");
mphotocaptureimageview.setImageBitmap(photocapturebitmap);
}
}
}

Вам также может понравиться