Taking Pictures/Video with Camera

0 Colm Hand · January 27, 2015
Hi guys,

I'm following tutorials 56/57 to try and use my device camera.  However, whenever I use the button my application crashes.  I pretty much used the code as in the tutorials and it doesn't work.  Any suggestions? Thanks

Post a Reply

Replies

Oldest  Newest  Rating
0 Colm Hand · January 27, 2015

package com.handco.myapplication;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.content.pm.PackageInfo;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.VideoView;


public class DisplayChestExercisesActivity extends ActionBarActivity {

    static final int REQUEST_IMAGE_CAPTURE = 1; //identifies intent
    //Intent camera_intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
    ImageView ChestImageView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_chest_exercises);
        Intent intent = getIntent();

        Button ChestRecordButton = (Button) findViewById(R.id.ChestRecordButton);
        ChestImageView = (ImageView) findViewById(R.id.ChestImageView);

        //disable button if device has no camera
        if(!hasCamera()) {
            ChestRecordButton.setEnabled(false);
        }

    }

    //check if device has a camera
    private boolean hasCamera(){
        return getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY);
    }



    //Launching camera
    public void launchCamera(){
        Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        //Record video and pass video results (data) to onActivityResult
        startActivityForResult(camera_intent, REQUEST_IMAGE_CAPTURE);
    }

    //If you want to return video and perform (vision) actions on it
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
       if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
          Bundle extras = data.getExtras();
           Bitmap photo = (Bitmap) extras.get("data");
           ChestImageView.setImageBitmap(photo);

       }
    }
}
0 Colm Hand · January 27, 2015

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

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Record Video"
        android:id="@+id/ChestRecordButton"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:nestedScrollingEnabled="false"
        android:onClick="launchCamera"
        android:clickable="false" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/ChestImageView"
        android:layout_centerVertical="true"
        android:layout_alignRight="@+id/ChestRecordButton"
        android:layout_alignEnd="@+id/ChestRecordButton"
        android:layout_marginRight="61dp"
        android:layout_marginEnd="61dp" />

</RelativeLayout>
  • 1

Java / Android Development

107,020 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator