3

I'm trying to use the next code in order to get a picture from the camera -

private void openImageIntent() {

    // Determine Uri of camera image to save.
    final File root = new File(Environment.getExternalStorageDirectory() + File.separator + "MyDir" + File.separator);
    root.mkdirs();
    //final String fname = Utils.getUniqueImageFilename();

    long elapsedMsec = System.currentTimeMillis();

     final String fname = "img_"+ System.currentTimeMillis() + ".jpg";
    final File sdImageMainDirectory = new File(root, fname);
    outputFileUri = Uri.fromFile(sdImageMainDirectory);

        // Camera.
        final List<Intent> cameraIntents = new ArrayList<Intent>();
        final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        final PackageManager packageManager = getPackageManager();
        final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
        for(ResolveInfo res : listCam) {
            final String packageName = res.activityInfo.packageName;
            final Intent intent = new Intent(captureIntent);
            intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
            intent.setPackage(packageName);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
            cameraIntents.add(intent);
        }

        // Filesystem.
        final Intent galleryIntent = new Intent();
        galleryIntent.setType("image/*");
        galleryIntent.setAction(Intent.ACTION_GET_CONTENT);

        // Chooser of filesystem options.
        final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");

        // Add the camera options.
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{}));

        startActivityForResult(chooserIntent, 1);
    }

And when the Intent is finished, this is the code -

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

      if(resultCode == RESULT_OK)
        {
            if(requestCode == 1)
            {
                final boolean isCamera;
                if(data == null)
                {
                    isCamera = true;
                }
                else
                {
                    final String action = data.getAction();
                    if(action == null)
                    {
                        isCamera = false;
                    }
                    else
                    {
                        isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                    }
                }

                Uri selectedImageUri;
                if(isCamera)
                {

                    selectedImageUri = outputFileUri;
                    currImageURI = selectedImageUri;

                }
                else
                {
                    selectedImageUri = data == null ? null : data.getData();
                    currImageURI = selectedImageUri;

                }
            }
        }

}

Now I trying to save the image uri in a global variable naming it *currImageURI *

I even tried to use this part of code, but it seem to crusing the app -

  if(isCamera)
            {

               currImageURI = data.getData();

            }

And I even added to the manifest the next lines

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

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

Now in picking a picture from the gallery all is fine, but when trying to get picture from the camera I get a null URI.

Any ideas why?

Thanks for any kind of help

4this
  • 759
  • 4
  • 13
  • 27
  • 1
    Yeah... You will receive `null` only. your captured image will be saved at a location that you pass as an extra parameter with the key `MediaStore.EXTRA_OUTPUT`... – Gopal Gopi Mar 21 '14 at 11:14
  • Hope the following links work for you: http://stackoverflow.com/questions/9890757/android-camera-data-intent-returns-null http://stackoverflow.com/questions/6982184/camera-activity-returning-null-android – Farhan Shah Mar 21 '14 at 11:20
  • Have you added permission for access external storage. (WRITE_EXTERNAL_STORAGE) ? – Vijju Mar 21 '14 at 11:22
  • But it seems that [outputFileUri} is passing with the intent, and been reused at [onActivityResult]. And yes I've used - WRITE_EXTERNAL_STORAGE – 4this Mar 21 '14 at 11:22

1 Answers1

0

I tried this code and I had the same problem

The simple solution is to add static type to the public variable like this

private static Uri outputFileUri;

Because when the Camera opens, the activity will close and the link is deleted from the variable