3

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
Matteo
  • 7,924
  • 24
  • 84
  • 129
  • 1
    what is the logcat saying about the problem – G_S Sep 24 '12 at 19:20
  • @SharathG - found something interesting, it seems as if it is the function getPath() that is causing the java null pointer exception...can you help me?thks in advance – Matteo Sep 24 '12 at 19:40
  • once check out whether the uri is having the value or not. I think uri doesnot have the path which is causing nullpointer – G_S Sep 24 '12 at 19:44
  • @SharathG - shouldn't uri be set by the content provider? – Matteo Sep 24 '12 at 19:45
  • no its not the one i am saying. Where did you call getpath(uri) ? – G_S Sep 24 '12 at 19:47
  • 1
    Your logcat seems to indicate that this has nothing to do with the camera, but rather that there is a null pointer floating around somewhere. I would recommend editing your post (title and all) to accurately reflect the actual problem and eliminate extraneous information. – Bryan Herbst Sep 24 '12 at 19:47
  • have you tried using `CursorLoader` ? – Codeman Sep 24 '12 at 19:48
  • once read Jamesandresakis's answer. – G_S Sep 24 '12 at 19:52
  • @Tanis.7x - I printed the ImageUri returned by the camera intent and it is null, this is the problem in my opinion, butI do not know why. Can you help? selectedImageUri = imageReturnedIntent.getData(); Log.d( TAG, selectedImageUri ); – Matteo Sep 24 '12 at 19:54
  • what is imageReturnedIntent? – G_S Sep 24 '12 at 19:57
  • @SharathG - posted complete code for understanding – Matteo Sep 24 '12 at 20:00
  • what did you get here Log.d( TAG, selectedImageUri ); ? – G_S Sep 24 '12 at 20:02
  • D/DVA_HLUISuperviseActivity(32722): null, as I said in some comment above it is the camera content provider that will not work, I just do not understand why... – Matteo Sep 24 '12 at 20:04
  • once have a look at http://stackoverflow.com/questions/11927710/get-image-from-capture-and-show-image-in-another-layout-using-another-activity-i/11927947#11927947 discussion. It may be helpful to you – G_S Sep 24 '12 at 20:08

3 Answers3

2

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.

2

Apparently this crash is due to a known Samsung problem: it seems like you need to create a Uri before calling the camera intent, in this way when running the onActivityResult method the content provider will allready have allocated the place where to save the resource.

For further information check the following:

and many more by googling...

P.S. as soon as possible I'll post the solution that worked for me.

Community
  • 1
  • 1
Matteo
  • 7,924
  • 24
  • 84
  • 129
0

I know it is late to answer it but asap i found the answer i replied for it so please atleast review it .

AndroidCameraUtil could be nice and easy solution for each and every device below is the code snippet you can use with the library

 private void setupCameraIntentHelper() {
    mCameraIntentHelper = new CameraIntentHelper(this, new CameraIntentHelperCallback() {
        @Override
        public void onPhotoUriFound(Date dateCameraIntentStarted, Uri photoUri, int rotateXDegrees) {
            messageView.setText(getString(R.string.activity_camera_intent_photo_uri_found) + photoUri.toString());

            Bitmap photo = BitmapHelper.readBitmap(CameraIntentActivity.this, photoUri);
            if (photo != null) {
                photo = BitmapHelper.shrinkBitmap(photo, 300, rotateXDegrees);
                ImageView imageView = (ImageView) findViewById(de.ecotastic.android.camerautil.sample.R.id.activity_camera_intent_image_view);
                imageView.setImageBitmap(photo);
            }
        }

        @Override
        public void deletePhotoWithUri(Uri photoUri) {
            BitmapHelper.deleteImageWithUriIfExists(photoUri, CameraIntentActivity.this);
        }

        @Override
        public void onSdCardNotMounted() {
            Toast.makeText(getApplicationContext(), getString(R.string.error_sd_card_not_mounted), Toast.LENGTH_LONG).show();
        }

        @Override
        public void onCanceled() {
            Toast.makeText(getApplicationContext(), getString(R.string.warning_camera_intent_canceled), Toast.LENGTH_LONG).show();
        }

        @Override
        public void onCouldNotTakePhoto() {
            Toast.makeText(getApplicationContext(), getString(R.string.error_could_not_take_photo), Toast.LENGTH_LONG).show();
        }

        @Override
        public void onPhotoUriNotFound() {
            messageView.setText(getString(R.string.activity_camera_intent_photo_uri_not_found));
        }

        @Override
        public void logException(Exception e) {
            Toast.makeText(getApplicationContext(), getString(R.string.error_sth_went_wrong), Toast.LENGTH_LONG).show();
            Log.d(getClass().getName(), e.getMessage());
        }
    });
}

@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    mCameraIntentHelper.onSaveInstanceState(savedInstanceState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    mCameraIntentHelper.onRestoreInstanceState(savedInstanceState);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    mCameraIntentHelper.onActivityResult(requestCode, resultCode, intent);
}
}
Hardy
  • 2,576
  • 1
  • 23
  • 45