Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application Crash when using CAMERA - Android

I've written an application which should take a picture and then show it on the screen for modifications. When trying it on the eclipse emulator the camera won't work, so I'm trying it on my Galaxy Nexus Smart Phone.

Nevertheless when running it on my SP the application will crash saying that it unfortunally stopped working.

When executing the app this is what exactly happens:

  1. I click on the camera button and the camera interface gets opened
  2. After taking the picture it gives me the choice to discard it or open it
  3. If I click on discard the application returns to normal usage
  4. If I click on open the application crashes as mentioned above

I googled a little and found out that you need permissions to use hardware devices check here, so I created the file /etc/udev/rules.d/51-android.rules and this is its content:

SUBSYSTEM=="USB", ATTR{IDVENDOR}=="18d1", MODE="0666, "GROUP="plugdev" SUBSYSTEM=="USB", ATTR{IDVENDOR}=="04e8", MODE="0666, "GROUP="plugdev" SUBSYSTEM=="USB", ATTR{IDVENDOR}=="0bb4", MODE="0666, "GROUP="plugdev"

But still I won't be able to use camera.

Here are the permissions I declared in my manifest file:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

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

Here is the code I use to launch the camera intent:

//create new Intent     
Intent cameraIntent = new Intent( android.provider.MediaStore.ACTION_IMAGE_CAPTURE );

//get something back from the activity we are starting
startActivityForResult( cameraIntent, CAMERA_PICTURE_REQUEST );

And this is the code for processing the result:

public void onActivityResult( int requestCode, int resultCode, Intent imageReturnedIntent ) 
{

    if( resultCode == RESULT_OK ) 
    {
        if( requestCode == GALLERY_PICTURE_REQUEST ) 
        {


            selectedImageUri = imageReturnedIntent.getData();

            Log.d( TAG, selectedImageUri );

            Intent intent = new Intent( DVAHLUI_SuperviseActivity.this, DVAHLUI_SelectImageContentActivity.class );


            intent.setData( selectedImageUri );
            startActivity( intent );


        }

        if( requestCode == CAMERA_PICTURE_REQUEST ) 
        {


            selectedImageUri = imageReturnedIntent.getData();
            Log.d( TAG, selectedImageUri );

            Intent intent = new Intent( DVAHLUI_SuperviseActivity.this, DVAHLUI_SelectImageContentActivity.class );


            intent.setData( selectedImageUri );
            startActivity( intent );


        }
    }
}

This is the getPath() function causing the Java Null pointer exception:

public String getPath( Uri uri ) 
{
    String[] filePathColumn = { android.provider.MediaStore.Images.Media.DATA };

LINE 343 -->    Cursor cursor = getContentResolver().query( uri, filePathColumn, null, null, null );
    cursor.moveToFirst();

    int columnIndex = cursor.getColumnIndexOrThrow( filePathColumn[0] );
    String filePath = cursor.getString( columnIndex );

    cursor.close();

    return filePath;

}

Can you please tell me what's going wrong?

FOGOT TO POST LOGCAT:

E/AndroidRuntime(27859): FATAL EXCEPTION: main
E/AndroidRuntime(27859): java.lang.RuntimeException: Failure delivering result ResultInfo{who=supervise, request=1, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.DVA_HLUI/com.DVA_HLUI.DVAHLUI_TabModeActivity}: java.lang.NullPointerException 
E/AndroidRuntime(27859):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3141)
E/AndroidRuntime(27859):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3184)
E/AndroidRuntime(27859):    at android.app.ActivityThread.access$1100(ActivityThread.java:130)
E/AndroidRuntime(27859):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)
E/AndroidRuntime(27859):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(27859):    at android.os.Looper.loop(Looper.java:137) E/AndroidRuntime(27859):     at android.app.ActivityThread.main(ActivityThread.java:4745)
E/AndroidRuntime(27859):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(27859):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(27859):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
E/AndroidRuntime(27859):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
E/AndroidRuntime(27859):    at dalvik.system.NativeStart.main(Native Method) 
E/AndroidRuntime(27859): Caused by: java.lang.NullPointerException 
E/AndroidRuntime(27859):    at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1094)
E/AndroidRuntime(27859):    at android.content.ContentResolver.query(ContentResolver.java:354)
E/AndroidRuntime(27859):    at android.content.ContentResolver.query(ContentResolver.java:313)
E/AndroidRuntime(27859):    at com.DVA_HLUI.DVAHLUI_SuperviseActivity.getPath(DVAHLUI_SuperviseActivity.java:343)
E/AndroidRuntime(27859):    at com.DVA_HLUI.DVAHLUI_SuperviseActivity.onActivityResult(DVAHLUI_SuperviseActivity.java:312)
E/AndroidRuntime(27859):    at android.app.ActivityGroup.dispatchActivityResult(ActivityGroup.java:122)
E/AndroidRuntime(27859):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3137)
E/AndroidRuntime(27859):    ... 11 more
like image 752
Matteo Avatar asked Jul 11 '26 19:07

Matteo


1 Answers

It sounds and looks like your not allowing the system to update itself that a new media file has been created. Thats why your method is failing. You can either manully create the image file path so you have the images location on the file tree or you can call for the media service to run an update. I always create my own filepath as older phones take longer to update using the media service and so your method in that case would fail.

like image 61
Jamesandresakis Avatar answered Jul 13 '26 09:07

Jamesandresakis