2

I'm trying to use my own custom implementation of CWAC Camera. I am trying to override the saveImage function on my own extension of the SimpleCameraHost. This is my code:

You are welcome to override saveImage(byte[]) and do something else with the byte[], such as send it over the Internet. saveImage(byte[]) is called on a background thread, so you do not have to do your own asynchronous work.

@Override
public void saveImage(byte[] byteArray) {
    Intent myIntent = new Intent();
    retData.putExtra("data", byteArray);
    getActivity().setResult(Activity.RESULT_OK, myIntent);
    getActivity().finish();
}

I'm testing this on a samsung galaxy S3, and it works perfectly, but when I try to use the front facing camera, my application freezes up and crashes. I'm not doing anything onActivityResult, so it is originating from the CameraFragment. Don't know what could be wrong because the documentation says that it has been tested with an S3.

EDIT #2:

@Override
public Size getPictureSize(Parameters parameters) {

        //return CameraUtils.getLargestPictureSize(parameters);
        // TODO Auto-generated method stub
        List<Camera.Size> list = parameters.getSupportedPictureSizes();
        int targetWidth = 1280;
        int width;
        Camera.Size optimalSize= null;
        for (Camera.Size size : list) {
            width = size.width;
            if (width==targetWidth) {
                optimalSize = size;
                return optimalSize;
            } else if (width<targetWidth){
                optimalSize = size;
                return optimalSize;
            } else if (width>targetWidth) {
                optimalSize = size;
            }
        }

        return optimalSize;

}

Okay, so my code definitely works for passing this through for the back camera. When I try the front facing camera, I still get a crash. If this was a binder error wouldn't I get !!! FAILED BINDER TRANSACTION !!!

Stacktrace:

08-12 17:36:40.559: E/YYYY(1295): Height: 960 Width: 1280
08-12 17:36:40.969: D/DeviceProfile(1295): m0xx
08-12 17:36:40.969: I/CWAC-Camera(1295): begin rotateForRealz()
08-12 17:36:40.994: D/dalvikvm(1295): GC_FOR_ALLOC freed 1937K, 8% free 40478K/43911K, paused 26ms, total 26ms
08-12 17:36:40.994: D/AbsListView(1295): [unregisterDoubleTapMotionListener]
08-12 17:36:41.004: I/dalvikvm-heap(1295): Grow heap (frag case) to 44.943MB for 4915216-byte allocation
08-12 17:36:41.039: D/dalvikvm(1295): GC_FOR_ALLOC freed 5K, 8% free 45273K/48775K, paused 34ms, total 34ms
08-12 17:36:41.059: I/MotionRecognitionManager(1295):   .unregisterListener : / listener count = 0->0, listener=android.widget.AbsListView$4@436bd2e8
08-12 17:36:41.074: D/dalvikvm(1295): GC_CONCURRENT freed 21K, 8% free 45253K/48775K, paused 12ms+3ms, total 35ms
08-12 17:36:41.104: D/dalvikvm(1295): WAIT_FOR_CONCURRENT_GC blocked 0ms
08-12 17:36:41.129: D/dalvikvm(1295): GC_EXPLICIT freed <1K, 8% free 45252K/48775K, paused 2ms+3ms, total 26ms
08-12 17:36:41.149: D/dalvikvm(1295): GC_FOR_ALLOC freed 1K, 8% free 45256K/48775K, paused 15ms, total 15ms
08-12 17:36:41.154: I/dalvikvm-heap(1295): Grow heap (frag case) to 49.608MB for 4915216-byte allocation
08-12 17:36:41.184: D/dalvikvm(1295): GC_CONCURRENT freed 277K, 8% free 49778K/53639K, paused 11ms+4ms, total 29ms
08-12 17:36:41.184: D/dalvikvm(1295): WAIT_FOR_CONCURRENT_GC blocked 17ms
08-12 17:36:41.329: I/CWAC-Camera(1295): end rotateForRealz()
08-12 17:36:41.529: D/dalvikvm(1295): WAIT_FOR_CONCURRENT_GC blocked 0ms
08-12 17:36:41.559: D/dalvikvm(1295): GC_EXPLICIT freed 10806K, 24% free 40787K/53639K, paused 2ms+4ms, total 29ms
08-12 17:36:41.584: D/CameraView(1295): stopping preview
08-12 17:36:41.719: D/CameraView(1295): releasing camera
08-12 17:36:41.864: D/dalvikvm(3251): WAIT_FOR_CONCURRENT_GC blocked 0ms
08-12 17:36:41.899: D/dalvikvm(3268): WAIT_FOR_CONCURRENT_GC blocked 0ms
08-12 17:36:41.939: D/dalvikvm(3268): Trying to load lib /data/data/com.myapp.sample/lib/myappScore.so 0x429c6cf8
08-12 17:36:41.939: D/dalvikvm(3268): Added shared lib /data/data/com.myapp.sample/lib/myappScore.so 0x429c6cf8
08-12 17:36:41.939: D/dalvikvm(3268): No JNI_OnLoad found in /data/data/com.myapp.sample/lib/myappScore.so 0x429c6cf8, skipping init
08-12 17:36:41.969: I/ActionBarSherlock(3268): Registering ActionBarSherlockCompat with qualifier @com.actionbarsherlock.ActionBarSherlock$Implementation(api=7, dpi=-1)
08-12 17:36:41.969: I/ActionBarSherlock(3268): Registering ActionBarSherlockNative with qualifier @com.actionbarsherlock.ActionBarSherlock$Implementation(api=14, dpi=-1)
08-12 17:36:41.969: I/ActionBarSherlock(3268): Using implementation: ActionBarSherlockNative
08-12 17:36:41.969: D/ActionBarSherlock(3268): [<ctor>] activity: com.myapp.gui.details.ConvoDetailActivity@42a161a8, flags: 1
08-12 17:36:41.984: D/ActionBarSherlock(3268): [setContentView] layoutResId: 2130903062
08-12 17:36:42.004: D/dalvikvm(3268): GC_CONCURRENT freed 191K, 8% free 12330K/13319K, paused 13ms+12ms, total 36ms
08-12 17:36:42.019: D/dalvikvm(3268): GC_FOR_ALLOC freed 2K, 8% free 12330K/13319K, paused 10ms, total 10ms
08-12 17:36:42.019: I/dalvikvm-heap(3268): Grow heap (frag case) to 13.102MB for 353328-byte allocation
08-12 17:36:42.029: D/dalvikvm(3268): GC_FOR_ALLOC freed <1K, 8% free 12675K/13703K, paused 8ms, total 8ms
08-12 17:36:42.064: D/dalvikvm(3268): GC_FOR_ALLOC freed 5K, 8% free 12703K/13703K, paused 11ms, total 11ms
08-12 17:36:42.064: I/dalvikvm-heap(3268): Grow heap (frag case) to 15.473MB for 2457616-byte allocation
08-12 17:36:42.089: D/dalvikvm(3268): GC_CONCURRENT freed <1K, 7% free 15102K/16135K, paused 11ms+2ms, total 23ms
08-12 17:36:42.114: D/dalvikvm(3268): GC_FOR_ALLOC freed 0K, 7% free 15102K/16135K, paused 11ms, total 11ms
08-12 17:36:42.129: I/dalvikvm-heap(3268): Grow heap (frag case) to 24.847MB for 9830416-byte allocation
08-12 17:36:42.154: D/dalvikvm(3268): GC_CONCURRENT freed 0K, 5% free 24702K/25799K, paused 12ms+2ms, total 25ms
08-12 17:36:42.184: D/dalvikvm(3268): GC_FOR_ALLOC freed 2400K, 14% free 22725K/26247K, paused 12ms, total 12ms
08-12 17:36:42.234: D/dalvikvm(3268): GC_CONCURRENT freed 452K, 8% free 24310K/26247K, paused 3ms+12ms, total 26ms
08-12 17:36:42.234: D/AbsListView(3268): Get MotionRecognitionManager
08-12 17:36:42.244: D/AndroidRuntime(3268): Shutting down VM
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • "When I try the front facing camera, I still get a crash" -- use LogCat to examine the Java stack trace with your crash. – CommonsWare Aug 12 '13 at 21:32
  • I'm trying but all I get is a rotatingForRealz log that throws me off. Not sure what that means because I'm not rotating. –  Aug 12 '13 at 21:33
  • "all I get is a rotatingForRealz log that throws me off" -- if that's a stack trace, **post it**. "Not sure what that means because I'm not rotating" -- but you are holding the device in portrait mode. Samsung devices will store the image in landscape, despite the way you hold the device, so I rotate the image for you, for consistent output between devices. – CommonsWare Aug 12 '13 at 21:36
  • Okay posting the stack trace now. –  Aug 12 '13 at 21:38
  • That's not a stack trace. Those are simple log messages (admittedly, ones I should get rid of). – CommonsWare Aug 12 '13 at 21:45
  • Okay, I thought thats what you were after. Let me update it with some more. –  Aug 12 '13 at 21:47
  • That is everything from the logcat from the takePicture() method till it crahes. –  Aug 12 '13 at 21:49
  • Hmm... if I take a picture using the Front Facing Camera first, then it doesn't crash. EDIT: Seemed to have been a fluke. Tried again and it still crashes. I did get it working once though. –  Aug 12 '13 at 21:51
  • I suggest you log the Size selected by getPictureSize() function. From the logic, it may happens that when using the back camera, it match with a very low resolution while with front camera, it match with a max resolution. The problems seems to be these lines: `else if (width – John Pang Nov 23 '14 at 00:23

1 Answers1

1

I'm testing this on a samsung galaxy S3, and it works perfectly, but when I try to use the front facing camera, my application freezes up and crashes.

In the future, use LogCat to examine the Java stack trace with your crash.

In this case, my guess is that it is because you are trying to pass a multi-MB image file in an Intent extra, which will not work. Either use the image in the same activity where you take it, or pass it using other means (e.g., a careful handoff via a static data member).

Don't know what could be wrong because the documentation says that it has been tested with an S3.

And if you doubt this, try not overriding saveImage(), and see what happens.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I'm a little confused on how the front facing camera (Around 2 MP) is taking up too much MB, and the back facing camera which is over 5MP is working fine. Additionally please check my edit #1. –  Aug 12 '13 at 21:16
  • @user2676468: There's a ~1MB limit on a Binder transaction, which underlies the `Intent` system, so neither camera should work. You'll notice that `ACTION_IMAGE_CAPTURE`, for example, does not return a full-resolution image in an `Intent` extra, but rather writes it to a file. – CommonsWare Aug 12 '13 at 21:18
  • Neither camera "should" work, but I'm testing this on other devices now and getting the same problem. The front facing camera crashes the application. Surely there is a way to test if I can send the data over as an Extra? I believe that the library may be the cause of this though. I really don't understand why FFCameras are giving me this issue. –  Aug 12 '13 at 21:23
  • @user2676468: "I believe that the library may be the cause of this though" -- create a sample app that reproduces the problem and that doesn't attempt to shove an image into an `Intent` extra, and I'll be happy to take a look at it. For example, change your code to return some tiny static `byte[]` as the extra. Or, reproduce the problem with the project's supplied sample apps, as I suggested in my answer. But the better answer is to rewrite your app to not use large `Intent` extras in the first place: http://code.google.com/p/android/issues/detail?id=5878 – CommonsWare Aug 12 '13 at 21:28
  • @user2676468: See also https://groups.google.com/forum/#!topic/android-developers/kwmTGz8GAoQ and http://stackoverflow.com/questions/12496700/maximum-length-of-intent-putextra-method-force-close. Also, your edit #2 is empty. – CommonsWare Aug 12 '13 at 21:30
  • @user2676468: Switching commentary back to the answer, there is nothing in LogCat that indicates the specific source of your problem. Again, you're welcome to demonstrate a problem with the library that doesn't involve multi-megabyte `Intent` extras, and I'll be happy to look into it. Otherwise, please try avoiding the extras, to see if that does indeed help your situation. – CommonsWare Aug 12 '13 at 21:53
  • How would you recommend possibly sending the byte[] over the air? As mentioned `You are welcome to override saveImage(byte[]) and do something else with the byte[], such as send it over the Internet.` I would prefer not to save it, but I would like to do it in another activity. Would you recommend opening up the camera fragment in a dialog? –  Aug 12 '13 at 21:57
  • @user2676468: "How would you recommend possibly sending the byte[] over the air?" -- you're on a background thread, so do an HTTP operation to your server of choice. Or, if you would prefer to delegate this to an `IntentService`, create a work queue singleton to hold onto the image(s) to be uploaded, then send a command via `startService()` to kick off the uploading work. "Would you recommend opening up the camera fragment in a dialog?" -- I have never tried that. I still don't see why you are having difficulty with using a static data member as a workaround to the `Intent` size limits. – CommonsWare Aug 12 '13 at 22:00
  • I will try the static data member now and will respond back with any issues. "My difficulty" with using a static data member workaround is simply because I don't want to believe that the higher MP camera works fine, while the lower MP camera gives me the problem =) –  Aug 12 '13 at 22:03
  • Doing some tests now. Worked on the first image. Died on the second. –  Aug 12 '13 at 22:08
  • Yeah, I'm getting a SensorManager crash every now and then. Oh well. This is more stable than what I had before. –  Aug 12 '13 at 22:11
  • Check LogCat to see if it died in a similarly silent fashion. Then, add your own Log statements (or breakpoints) to try to get a better handle on where it is dying. CWAC-Camera isn't directly touching `SensorManager`, though it does use an orientation listener, which under the covers presumably uses `SensorManager`. If the stack trace for that is showing that the `SensorManager` crashes seem tied to CWAC-Camera code, I'd be interested in seeing it. – CommonsWare Aug 12 '13 at 22:11
  • I will definitely be on the lookout for any problems and will try to handle it. I don't use any sensor in my application which is why "SensorManager" throws up a flag for me. Thanks again for the help. –  Aug 12 '13 at 22:21