Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
385 views
in Technique[技术] by (71.8m points)

callback - PictureCallback not called when using takePicture in Android

I'm taking a picture using my own camera and trying to save the result to the file system (/data/data/package.name/file/captured_photo1.jpeg). When i try it on the emulator (Android 2.2) it seems to work - creates a file with size 8733 bytes and displays it (emulator's default photo). The problem is that when i try it on my GalaxyS (Android 2.2.1) the image on the file system is 0 bytes

i found out (from the log) that one of the callbacks i pass to my takePicture method isn't called. and it's the one that actually saves it.

the data[] that i get in the rawCallback is null both on the phone and the emulator, but on the emulator the callback is called and it works

this is the whole Activity code:

public class SolveCaptureActivity extends Activity {
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.solve_capture);

        preview = new Preview(this);
        ((FrameLayout) findViewById(R.id.preview)).addView(preview);

        buttonClick = (Button) findViewById(R.id.buttonCapture);
        buttonClick.setOnClickListener(new OnClickListener() {
          public void onClick(View v) { 
            preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);
            Intent gotoImagePreview = new Intent(v.getContext(), CapturedImagePreview.class);
            startActivityForResult(gotoImagePreview, 0);
          }
        });

        buttonFocus = (Button) findViewById(R.id.buttonFocus);
        buttonFocus.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

              preview.camera.autoFocus(new AutoFocusCallback() {
                @Override
                public void onAutoFocus(boolean success, Camera camera) {
                    Camera.Parameters camParam = camera.getParameters();
                    camParam.setFocusMode(Parameters.FOCUS_MODE_AUTO);
                    camera.setParameters(camParam);
                }
              });
            }
        });

        Log.d(TAG, "onCreate'd");
      }

      // Called when shutter is opened
      ShutterCallback shutterCallback = new ShutterCallback() {
        public void onShutter() {
          Log.d(TAG, "onShutter'd");
        }
      };

      PictureCallback rawCallback = new PictureCallback() {
        public void onPictureTaken(byte[] data, Camera camera) {
          Log.d(TAG, "onPictureTaken - raw");
        }
      };

      PictureCallback jpegCallback = new PictureCallback() { // <8>
        public void onPictureTaken(byte[] data, Camera camera) {
          FileOutputStream outStream = null;
          if (data == null){
              Log.d("@@--DATA--@@, msg","=NULL");
          }
          try {         
            Log.d("@@--DIR--@@", Environment.getExternalStorageDirectory().getAbsolutePath());
            Log.d("@@--Size--@@", Integer.toString(data.length));
            outStream = getApplicationContext().openFileOutput(SolveCaptureActivity.FILE_NAME, Context.MODE_WORLD_READABLE);

            outStream.write(data);
            outStream.flush();
            outStream.close();
            Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
          } catch (FileNotFoundException e) { // <10>
            e.printStackTrace();
          } catch (IOException e) {
            e.printStackTrace();
          } finally {
          }
          Log.d(TAG, "onPictureTaken - jpeg");
        }
      };

    }

this is the log:

09-01 18:44:38.403: WARN/CameraService(6488): takePicture (pid 6585)
09-01 18:44:38.403: INFO/ShotSingle(6488): ShotSingle::takePicture start
09-01 18:44:38.403: ERROR/CameraHardwareSec(6488): stopPreview()
09-01 18:44:38.403: ERROR/SecCamera(6488): cancelAutofocus()
09-01 18:44:38.403: ERROR/SecCamera(6488): cancelAutofocus() end, 0, 4
09-01 18:44:38.409: ERROR/SecCamera(6488): stopPreview()
09-01 18:44:38.409: ERROR/SecCamera(6488): fimc_v4l2_streamoff()
09-01 18:44:38.433: ERROR/CameraHardwareSec(6488): stopPreview() end
09-01 18:44:38.433: INFO/ShotSingle(6488): ShotSingle::takePicture end
09-01 18:44:38.433: INFO/ActivityManager(3135): Starting activity: Intent { cmp=cs.workshop.solvedroid/.CapturedImagePreview }
09-01 18:44:38.437: WARN/CameraService(6488): setPreviewCallbackFlag (pid 6585)
09-01 18:44:38.437: DEBUG/Preview(6585): onPreviewFrame called at: 1314891878438
09-01 18:44:38.457: DEBUG/SecCamera(6488): passed fmt = 1498831189 found pixel format[3]: YUV 4:2:2 packed, CbYCrY
09-01 18:44:38.467: WARN/CameraService(6488): width(640), height(480), format:jpeg
09-01 18:44:38.523: WARN/System.err(6585): java.io.FileNotFoundException: /data/data/cs.workshop.solvedroid/files/captured_photo2.jpeg (No such file or directory)
09-01 18:44:38.542: WARN/System.err(6585):     at org.apache.harmony.luni.platform.OSFileSystem.openImpl(Native Method)
09-01 18:44:38.542: WARN/System.err(6585):     at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:152)
09-01 18:44:38.542: WARN/System.err(6585):     at java.io.FileInputStream.<init>(FileInputStream.java:82)
09-01 18:44:38.542: WARN/System.err(6585):     at android.app.ContextImpl.openFileInput(ContextImpl.java:448)
09-01 18:44:38.542: WARN/System.err(6585):     at android.content.ContextWrapper.openFileInput(ContextWrapper.java:152)
09-01 18:44:38.542: WARN/System.err(6585):     at cs.workshop.solvedroid.CapturedImagePreview.onCreate(CapturedImagePreview.java:29)
09-01 18:44:38.542: WARN/System.err(6585):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-01 18:44:38.543: WARN/System.err(6585):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-01 18:44:38.543: WARN/System.err(6585):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-01 18:44:38.545: WARN/System.err(6585):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-01 18:44:38.545: WARN/System.err(6585):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-01 18:44:38.547: WARN/System.err(6585):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-01 18:44:38.547: WARN/System.err(6585):     at android.os.Looper.loop(Looper.java:123)
09-01 18:44:38.549: WARN/System.err(6585):     at android.app.ActivityThread.main(ActivityThread.java:4627)
09-01 18:44:38.549: WARN/System.err(6585):     at java.lang.reflect.Method.invokeNative(Native Method)
09-01 18:44:38.551: WARN/System.err(6585):     at java.lang.reflect.Method.invoke(Method.java:521)
09-01 18:44:38.553: WARN/System.err(6585):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
09-01 18:44:38.553: WARN/System.err(6585):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
09-01 18:44:38.555: WARN/System.err(6585):     at dalvik.system.NativeStart.main(Native Method)
09-01 18:44:38.643: WARN/CameraService(6488): stopPreview (pid 6585)
09-01 18:44:38.643: ERROR/CameraHardwareSec(6488): stopPreview()
09-01 18:44:38.643: ERROR/SecCamera(6488): cancelAutofocus()
09-01 18:44:38.643: ERROR/SecCamera(6488): cancelAutofocus() end, 0, 4
09-01 18:44:38.643: ERROR/SecCamera(6488): stopPreview()
09-01 18:44:38.645: ERROR/SecCamera(6488): stopPreview: m_flag_camera_start is zero
09-01 18:44:38.645: ERROR/CameraHardwareSec(6488): stopPreview() end
09-01 18:44:38.645: WARN/CameraService(6488): stopPreview(), hardware stopped OK
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should wait jpegCallback.onPictureTaken() to be called and only then launch another activity:

PictureCallback jpegCallback = new PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
        // save data to file and if everything's OK
        Intent gotoImagePreview = new Intent(v.getContext(), CapturedImagePreview.class);
        startActivityForResult(gotoImagePreview, 0);
    }
};

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...