0

For an app, my team is currently working on I want to take a picture with the camera and display that in an ImageView in the same screen.

To do this I tried using an already answered question on Stackoverflow but it doesn't seem to work. I used this one >> Capture Image from Camera and Display in Activity

But whenever I ran the app and click on the ImageView or the button the app force quits. I really have no clue why app is force clossing.

Here is the code that I used in my activity java file:

package com.me.backtrack.backtrack;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.hardware.Camera;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class CameraActivity extends AppCompatActivity {
    private static final int CAMERA_REQUEST = 1888;
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
        this.imageView = (ImageView)this.findViewById(R.id.camera_preview);
        Button photoButton = (Button) this.findViewById(R.id.button_capture);
        photoButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(cameraIntent, CAMERA_REQUEST);
            }
        });
    }


    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
            Bitmap photo = (Bitmap) data.getExtras().get("data");
            imageView.setImageBitmap(photo);
        }
    }
}

And the following is my XML layout:

<?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.me.backtrack.backtrack.CameraActivity">


    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="587dp"
        android:layout_margin="10dp"
        android:orientation="vertical"
        android:weightSum="10"
        tools:layout_constraintTop_creator="1"
        tools:layout_constraintRight_creator="1"
        android:layout_marginStart="9dp"
        android:layout_marginEnd="9dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginTop="8dp"
        tools:layout_constraintLeft_creator="1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginLeft="9dp"
        android:layout_marginRight="9dp">

        <ImageView
            android:id="@+id/camera_preview"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="9"
            android:onClick="onClickCamera"/>

        <Button
            android:id="@+id/button_capture"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="Capture"
            android:onClick="onClickCapture"/>

    </LinearLayout>

</android.support.constraint.ConstraintLayout>

I am fairly new to android development and I tried many ways to fix this and it doesn't seem like I am doing it the right way.

Please let me know if you need more information

Thanks in advance!

UPDATE: ADDED CRASH LOG:

04-23 22:16:42.152 1577-1589/system_process I/ActivityManager: START u0 {cmp=com.me.backtrack.backtrack/.CameraActivity} from uid 10071 on display 0
04-23 22:16:42.785 1577-1600/system_process I/ActivityManager: Displayed com.me.backtrack.backtrack/.CameraActivity: +612ms
04-23 22:16:48.368 1577-1795/system_process W/ActivityManager:   Force finishing activity com.me.backtrack.backtrack/.CameraActivity
04-23 22:16:48.547 1577-2121/system_process I/ActivityManager: Killing 3043:com.google.android.apps.wallpaper/u0a60 (adj 906): empty #17
04-23 22:16:48.561 1577-1920/system_process D/ActivityManager: cleanUpApplicationRecord -- 3043
04-23 22:16:48.733 1577-3868/system_process I/OpenGLRenderer: Initialized EGL, version 1.4
04-23 22:16:48.734 1577-3868/system_process D/OpenGLRenderer: Swap behavior 1
04-23 22:16:48.885 1577-1591/system_process W/ActivityManager: Activity pause timeout for ActivityRecord{5aa8eed u0 com.me.backtrack.backtrack/.CameraActivity t43 f}
04-23 22:16:48.895 1577-3868/system_process E/EGL_emulation: tid 3868: eglSurfaceAttrib(1174): error 0x3009 (EGL_BAD_MATCH)
04-23 22:16:48.895 1577-3868/system_process W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x997bf7c0, error=EGL_BAD_MATCH
04-23 22:16:58.383 1577-1591/system_process W/ActivityManager: Launch timeout has expired, giving up wake lock!
04-23 22:16:58.947 1577-1591/system_process I/ActivityManager: Killing 3112:com.android.chrome/u0a30 (adj 906): empty #17
04-23 22:16:59.011 1577-1588/system_process D/ActivityManager: cleanUpApplicationRecord -- 3112
04-23 22:16:59.011 1577-1588/system_process W/ActivityManager: Scheduling restart of crashed service com.android.chrome/org.chromium.chrome.browser.customtabs.CustomTabsConnectionService in 1000ms
04-23 22:16:59.733 1577-1587/system_process I/art: Background sticky concurrent mark sweep GC freed 49633(2MB) AllocSpace objects, 9(272KB) LOS objects, 26% free, 10MB/14MB, paused 5.007ms total 66.404ms
04-23 22:17:00.051 1577-1591/system_process I/ActivityManager: Start proc 4042:com.android.chrome/u0a30 for service com.android.chrome/org.chromium.chrome.browser.customtabs.CustomTabsConnectionService
04-23 22:17:00.374 1577-2515/system_process I/ActivityManager: Killing 3134:com.google.process.gapps/u0a41 (adj 906): empty #17
04-23 22:17:00.384 1577-2260/system_process D/ActivityManager: cleanUpApplicationRecord -- 3134

UPDAT: ADDED MANIFEST:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.me.backtrack.backtrack">

    <uses-permission android:name="android.permission.INTERNET" />

    <!-- To auto-complete the email text field in the login form with the user's emails -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.READ_PROFILE" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.CAMERA" />
    //Camera Permissions
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    //For Local Storage
    <uses-feature android:name="android.hardware.camera"></uses-feature>
    //To access camera
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_logo_white"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".LoginActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".SignUpActivity" />
        <activity android:name=".HomeActivity" />
        <activity android:name=".SettingsActivity" />
        <activity
            android:name=".MainSettingsActivity"
            android:label="@string/title_activity_main_settings"
            android:parentActivityName=".HomeActivity">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.me.backtrack.backtrack.HomeActivity" />
        </activity>
        <activity android:name=".TagViewActivity" />
        <activity android:name=".CreateTrackActivity"></activity>
        <activity android:name=".TrackViewActivity"></activity>
        <activity android:name=".CameraActivity"></activity>
    </application>

</manifest>
Community
  • 1
  • 1
  • Post the stack trace with the crash. – Gabe Sechan Apr 24 '17 at 00:56
  • You should check if `data == null || data.getExtras() == null` before you ever use `data.getExtras()` or get something from there – OneCricketeer Apr 24 '17 at 01:03
  • Hi, i am sorry i just added the crash log – Sreekuttan Sudarsanan Apr 24 '17 at 02:18
  • Are you requesting the user's permission to use the cammera and external storage in you app before sending the intent to use the cammera? https://developer.android.com/training/permissions/requesting.html?hl=en – Juan Apr 24 '17 at 05:29
  • @Juan Isn't extra permission only required for newer android SDKs(6.0 +)? Our base APK is, I believe Android 4.4 KitKat. So wouldn't the permission statements in manifest be the only request for permission statements necessary? I might be wrong, I am pretty new to all of this. – Sreekuttan Sudarsanan Apr 24 '17 at 13:18
  • If your target sdk is 22 or lower you don't have to require permissions at run time. For your particular question, if your target sdk is below 23, it shouldn't be an issue not to ask for the additional permission. (Just a note of advice: the user of a device with Android 6 or newer could revoke manually the permissions granted during the installation) – Juan Apr 24 '17 at 13:28

0 Answers0