Sunday 10 November 2013

Image & Video Capturing Example

Here i am going to explain working with camera, in this example i am going to capture image/video and store them into SD card.

Example:










 Code:

activity_main.xml

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="90dp"
        android:text="Capture Video" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="70dp"
        android:text="Capture Image" />

</RelativeLayout>


MainActivity.java

package in.androiddevelopmentanddiscussion.imagevideocaptureexample;

import java.io.File;

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;

public class MainActivity extends Activity {
private static final int VIDEO_CAPTURE = 101;
private static final int IMAGE_CAPTURE = 102;
Button videocamera,imagecamera;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(!hasCamera()){
videocamera.setEnabled(false);
imagecamera.setEnabled(false);
Toast.makeText(MainActivity.this, "Your Camera Not Support This App", Toast.LENGTH_SHORT).show();
}
videocamera = (Button)findViewById(R.id.button1);
imagecamera = (Button)findViewById(R.id.button2);
videocamera.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
File mediaFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+ "/myvideo.mp4");
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
Uri videoUri = Uri.fromFile(mediaFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, videoUri);
startActivityForResult(intent, VIDEO_CAPTURE);
}
});
imagecamera.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
File imageFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+ "/myImage.jpg");
Intent imageIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri imageUri = Uri.fromFile(imageFile);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(imageIntent, IMAGE_CAPTURE);
}
});
}

//Checking the availability of camera
private boolean hasCamera() {
if (getPackageManager().hasSystemFeature(
          PackageManager.FEATURE_CAMERA)){
return true;
     } else {
             return false;
     }
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   if (requestCode == VIDEO_CAPTURE) {
 if (resultCode == RESULT_OK) {
    Toast.makeText(this, "Video saved to:\n" + data.getData(), Toast.LENGTH_LONG).show();
 } else if (resultCode == RESULT_CANCELED) {
    Toast.makeText(this, "Video recording cancelled.", Toast.LENGTH_LONG).show();
 } else {
    Toast.makeText(this, "Failed to record video", Toast.LENGTH_LONG).show();
       }
   }
}
}

Download full project from here



Check Android Apps on Google Play

https://play.google.com/store/apps/developer?id=Metro%20App%20Solution&hl=en

 

No comments:

Post a Comment