Search code examples
androidcameratemporary-files

Making a simple camera app


I made a simple camera app that was working just fine the other day. Now i tried it again today and i keep getting a nullpointer on the file. I used breakpoints in my code to see what goes wrong.

filefotoFile = maakFotoBestand();

.

    public File maakFotoBestand() throws IOException
{
    //maak een unieke filename aan aan de hand van een timestamp en zet de file storage klaar
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format (new Date());
    String imgFileName = "IMAGE_" + timeStamp + "_";
    File storageDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

    //sla de foto op in het geheugen aan de hand van de genoemde namen
    File image = File.createTempFile(imgFileName, ".JPG", storageDirectory);

    //Zeg de app waar de foto staat
    imageFileLocation = image.getAbsolutePath();

    return image;
}

He seems to stop at the line File image = File.create.........

He already returns at that line. And if I than look at the content of image its NULL Am I doing something wrong?

EDIT=================================================== Here is the entire code and the logcat

package nl.compict.cameraapp;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
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.ImageView;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends AppCompatActivity implements     View.OnClickListener
{

private static final int ACTIVITY_START_CAMERA_APP = 1;
private ImageView mFotoCapturedImageView;
private String imageFileLocation;

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

    mFotoCapturedImageView = (ImageView) findViewById(R.id.imageView01);
}


@Override
public void onClick(View v)
{
    Intent callCameraApplicationIntent = new Intent();

    //Roep de Android Camera App aan met de actie om een foto te maken
    callCameraApplicationIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);

    //
    File fotoFile = null;
    try
    {
        fotoFile = maakFotoBestand();
    }
    catch(IOException e)
    {
        //geef de error weer als die plaatsvind
        e.printStackTrace();
    }
    //roep de CameraApp nog een keer aan en onthoud het bestand dat is aangemaakt
    callCameraApplicationIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(fotoFile));

    //Start een activity met een request code zodat je die wee kan afvangen en wacht op een resultaat
    startActivityForResult(callCameraApplicationIntent, ACTIVITY_START_CAMERA_APP);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    //Kijk of de request en resultcode overeenkomen met wat wij willen doen
    if(requestCode == ACTIVITY_START_CAMERA_APP && resultCode == RESULT_OK)
    {
        //Haal de foto op aan de hand van de imageFileLocation
        Bitmap fotoCaptureBitmap = BitmapFactory.decodeFile(imageFileLocation);
        mFotoCapturedImageView.setImageBitmap(fotoCaptureBitmap);
    }
}

public File maakFotoBestand() throws IOException
{
    //maak een unieke filename aan aan de hand van een timestamp en zet de file storage klaar
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format (new Date());
    String imgFileName = "IMAGE_" + timeStamp + "_";
    File storageDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

    //sla de foto op in het geheugen aan de hand van de genoemde namen
    File image = File.createTempFile(imgFileName, ".JPG", storageDirectory);

    //Zeg de app waar de foto staat
    imageFileLocation = image.getAbsolutePath();

    return image;
}
}

Logcat

01-20 10:50:24.749 2398-2398/nl.compict.cameraapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                Process: nl.compict.cameraapp, PID: 2398
                                                                java.lang.IllegalStateException: Could not execute method for android:onClick
                                                                    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:275)
                                                                    at android.view.View.performClick(View.java:5198)
                                                                    at android.view.View$PerformClick.run(View.java:21147)
                                                                    at android.os.Handler.handleCallback(Handler.java:739)
                                                                    at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                    at android.os.Looper.loop(Looper.java:148)
                                                                    at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                 Caused by: java.lang.reflect.InvocationTargetException
                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270)
                                                                    at android.view.View.performClick(View.java:5198) 
                                                                    at android.view.View$PerformClick.run(View.java:21147) 
                                                                    at android.os.Handler.handleCallback(Handler.java:739) 
                                                                    at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                    at android.os.Looper.loop(Looper.java:148) 
                                                                    at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                                 Caused by: java.lang.NullPointerException: file
                                                                    at android.net.Uri.fromFile(Uri.java:452)
                                                                    at nl.compict.cameraapp.MainActivity.onClick(MainActivity.java:58)
                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270) 
                                                                    at android.view.View.performClick(View.java:5198) 
                                                                    at android.view.View$PerformClick.run(View.java:21147) 
                                                                    at android.os.Handler.handleCallback(Handler.java:739) 
                                                                    at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                    at android.os.Looper.loop(Looper.java:148) 
                                                                    at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

Solution

  • I tested your code, it works perfectly, you just need to add permissions inside your manifest :

    <manifest ...>
        <uses-permission android:name="android.permission.CAMERA"/>
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    </manifest>
    

    But I had to add a button to trigger the capture, like this :

    Inside your layout :

    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Take"
            android:id="@+id/button"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="59dp" />
    

    Inside your class :

    ...
    buttonTakePicture = (Button) findViewById(R.id.button);
    buttonTakePicture.setOnClickListener(this);
    ...
    @Override
        public void onClick(View v)
        {
            if(v == buttonTakePicture) {
                Intent callCameraApplicationIntent = new Intent();
    
                //Roep de Android Camera App aan met de actie om een foto te maken
                callCameraApplicationIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
    
                //
                File fotoFile = null;
                try {
                    fotoFile = maakFotoBestand();
                } catch (IOException e) {
                    //geef de error weer als die plaatsvind
                    e.printStackTrace();
                }
                //roep de CameraApp nog een keer aan en onthoud het bestand dat is aangemaakt
                callCameraApplicationIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(fotoFile));
    
                //Start een activity met een request code zodat je die wee kan afvangen en wacht op een resultaat
                startActivityForResult(callCameraApplicationIntent, ACTIVITY_START_CAMERA_APP);
            }
        }