How to Capture Android Camera Image?

Asked

Viewed 32 times

1

I’m having trouble opening the camera, which will capture images to take pictures and save them, in Android Studio using emulators. Follow the code below:

Cameraactivity.java

import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import java.io.File;

public class CameraActivity extends AppCompatActivity {
    private static final int CAPTURAR_IMAGEM = 1;
    private Uri uri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera);
}

@Override
protected void onActivityResult(int requestCode,
                                int resultCode, Intent data) {
    if (requestCode == CAPTURAR_IMAGEM) {
        if (resultCode == RESULT_OK) {
            mostrarMensagem("Imagem capturada!");
            adicionarNaGaleria();
        } else {
            mostrarMensagem("Imagem não capturada!");
        }
    }
}

private void mostrarMensagem(String msg){
    Toast.makeText(this, msg,
            Toast.LENGTH_LONG)
            .show();
}

private void adicionarNaGaleria() {
    Intent intent = new Intent(
            Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    intent.setData(uri);
    this.sendBroadcast(intent);
}

public void capturarImagem(View v){
    boolean temCamera = getPackageManager()
            .hasSystemFeature(PackageManager.FEATURE_CAMERA);
    if(temCamera) {
        File diretorio = Environment
                .getExternalStoragePublicDirectory(
                        Environment.DIRECTORY_PICTURES);
        String nomeImagem = diretorio.getPath() + "/" +
                System.currentTimeMillis() +
                ".jpg";
        uri = Uri.fromFile(new File(nomeImagem));

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        startActivityForResult(intent, CAPTURAR_IMAGEM);
    }
}
public void visualizarImagem(View v){
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(uri, "image/jpeg");
    startActivity(intent);
}
}

activity_camera.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
`xmlns:android="http://schemas.android.com/apk/res/android"`
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.testarcamera.CameraActivity">

<LinearLayout
    android:layout_width="368dp"
    android:layout_height="495dp"
    android:orientation="vertical"
    tools:layout_editor_absoluteX="8dp"
    tools:layout_editor_absoluteY="8dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btnCapturarImagem"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/capturarImagem"
            android:onClick="capturarImagem"/>

        <Button
            android:id="@+id/btnVisualizarImagem"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/visualizarImagem"
            android:onClick="visualizarImagem"/>
    </LinearLayout>
</LinearLayout>

Androidmanifest.xml

<uses-sdk
    android:maxSdkVersion="26"
    android:minSdkVersion="8" />

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

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".CameraActivity"></activity>
</application>

I searched several places, following step by step what they said, but I still haven’t found the error. It’s all going right, but when I click the button to open the camera and take a photo, it says the app stopped working and closes it. I put the permissions they said to put on Androidmanifest.xml, but it still didn’t work. I changed the settings of the emulators I use in the part of the camera to use Webcam0 and it didn’t work, so I put it to use Emulated and I didn’t. Can someone help me to know what the problem is and what to do? Thank you.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.