0

Every time I take a picture with my camera it sets selectedImageUri to null...

Also, DATA IS NOT NULL and ACTION IS NOT NULL get called, as to my confusion... I am not sure if I am doing something wrong let me know, the selectedImageUri when taking a picture should return as the Uri and not null

Here is my code:

 public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == getActivity().RESULT_OK) {
            if (requestCode == YOUR_SELECT_PICTURE_REQUEST_CODE) {
                final boolean isCamera;
                if (data == null) {
                    Log.e("data", "DATA IS NULL");
                    isCamera = true;
                } else {
                    Log.e("data", "DATA IS NOT NULL");
                    final String action = data.getAction();
                    //Log.e("PICTURE PATH?", action);
                    if (action == null) {
                        Log.e("data", "ACTION IS NULL");
                        isCamera = false;
                    } else {
                        Log.e("data", "ACTION IS NOT NULL");
                        isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

                    }
                }

                Uri selectedImageUri;
                if (isCamera) {
                    selectedImageUri = outputFileUri;
                    String uploadurl = "discover/p";
                    File photoSelected = new File(selectedImageUri.getPath());
                    TypedFile photo = new TypedFile("application/octet-stream", photoSelected);
                    String userId = "userId1";
                    Callback cameraResponse = new Callback() {
                        @Override
                        public void success(Object o, Response response) {

                        }

                        @Override
                        public void failure(RetrofitError retrofitError) {
                            Log.e("TAG", retrofitError.getMessage());
                        }
                    };
                    uploadImage(uploadurl, photo, userId, cameraResponse);
                    Picasso.with(getActivity()).load(selectedImageUri.getPath()).into(profilePicture);
                } else {
                    if (data == null) {
                        selectedImageUri = null;
                    } else {
                        selectedImageUri = data.getData();
                        Log.e("gallery image", "link is " + data.getData());
                        String uploadurl = "discover/p";
                        File photoSelected = new File(String.valueOf(selectedImageUri));
                        TypedFile photo = new TypedFile("application/octet-stream", photoSelected);
                        String userId = "userId1";
                        Callback galleryResponse = new Callback() {
                            @Override
                            public void success(Object o, Response response) {

                            }

                            @Override
                            public void failure(RetrofitError retrofitError) {

                            }
                        };
                        uploadImage(uploadurl, photo, userId, galleryResponse);

                        Picasso.with(getActivity()).load(selectedImageUri).into(profilePicture);
                    }

                }
            }
        }
    }

EDIT including camera code:

if(isDeviceSupportCamera()) {
                // Determine Uri of camera image to save.
                final File root = new File(Environment.getExternalStorageDirectory() + File.separator + "MyDir" + File.separator);
                root.mkdirs();
                final String fname = getUniqueImageFilename();
                final File sdImageMainDirectory = new File(root, fname);
                outputFileUri = Uri.fromFile(sdImageMainDirectory);
                Log.e("is camera ", "even getting CALLED CAM1111?");
                // Camera.
                final List<Intent> cameraIntents = new ArrayList<Intent>();
                Log.e("is camera ", "even getting CALLED CAM?222");
                final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                final PackageManager packageManager = getActivity().getPackageManager();
                final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
                Log.e("is camera ", "even getting CALLED CAM333?");
                for (ResolveInfo res : listCam) {
                    Log.e("is camera ", "even getting CALLED CAM4444?");
                    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);
                }
                Log.e("is camera ", "even getting CALLED CAM?5555");

                final Intent chooserCameraIntent = Intent.createChooser(captureIntent, "Use Camera");

                startActivityForResult(chooserCameraIntent, YOUR_SELECT_PICTURE_REQUEST_CODE);

                } else {
                     Toast.makeText(getActivity(), "There is no camera available on this device.", Toast.LENGTH_SHORT).show();
                }

            }

UPDATE

Log File:

03-12 10:20:21.854  27814-27814/com.example.app E/data﹕ CAMERA IS NOT NULL outputfileuri is file:///storage/emulated/0/MyDir/img_userID1%20_1394612413661.jpg
03-12 10:20:21.854  27814-27814/com.example.app E/data﹕ camera link for selectedImageUri file:///storage/emulated/0/MyDir/img_userID1%20_1394612413661.jpg
03-12 10:20:21.854  27814-27814/com.example.app E/data﹕ selected image get path link is /storage/emulated/0/MyDir/img_userID1 _1394612413661.jpg

I also added true to isCamera instead of

action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

if I add this code under my if request code

 if (resultCode == getActivity().RESULT_OK) {
            if (requestCode == YOUR_SELECT_PICTURE_REQUEST_CODE) {
//                Bundle extras = data.getExtras();
//                Bitmap imageBitmap = (Bitmap) extras.get("data");
//                profilePicture.setImageBitmap(imageBitmap);
               final boolean isCamera;

It actually shows the image in the image view... so I am confused as to why it is not getting the uri right.

This is the android manifest permission

<!-- For Camera use and downloading image -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <!-- internet -->
    <uses-permission android:name="android.permission.INTERNET" />
Lion789
  • 4,402
  • 12
  • 58
  • 96
  • Best guess: outputFileUri is null before it's used to set selectedImageUri. Since you haven't posted the relevant code about outputFileUri, there's not enough information to help you. – DigCamara Mar 11 '14 at 23:52
  • Hey updated the post by including the code for the camera, it is getting initialized before that as private Uri outputFilUri – Lion789 Mar 11 '14 at 23:54
  • are you sure it's got the proper value? Have you included the permissions you need? – DigCamara Mar 12 '14 at 00:19
  • E/JHEAD﹕ can't open '/storage/emulated I am getting that error this is what I get I added it to the bottom. as I edited my code a bit – Lion789 Mar 12 '14 at 08:22
  • When you're using Log.e to log your mesages, they appear as errors in the log. You actually should be using Log.d or Log.i. You should also post the permissions you're adding to your manifest (AndroidManifest.xml) You probably forgot to add permissions to external files. – DigCamara Mar 12 '14 at 15:30
  • Yeah, I know, and I included the manifest. – Lion789 Mar 12 '14 at 17:32
  • I see you are running the emulated version. What OS are you using? – DigCamara Mar 12 '14 at 18:49
  • what do you mean emulated, I am testing it with my device (nexus 5)? – Lion789 Mar 12 '14 at 18:54
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/49597/discussion-between-digcamara-and-lion789) – DigCamara Mar 12 '14 at 18:55

1 Answers1

1

Your activity could have been destroyed by the system to free memory for the Camera app to handle your intent. You can save and the state with onSaveInstanceState() and restore it in onCreate(). See e.g. Android startCamera gives me null Intent and ... does it destroy my global variable?.

Community
  • 1
  • 1
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • Hey I actually changed my code to this http://stackoverflow.com/questions/22365783/camera-is-not-saving-android and am calling onActivity created, I tried your suggestion of flipping it around to have onCreate after Activity results but it is giving me the same results... and I used the fileurl as well... I am going to mark you correct as it is something that I should have done but can you please help me on the other question – Lion789 Mar 12 '14 at 23:31