0

I developed a very basic camera app, so far it only contains the camera preview and a button to take a photo which is then saved. I am now wondering why I would run into a warning that my application might be doing to much work on it's main thread (it skips 35 frames). All I do so far on my UI thread can be seen in my onCreate():

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    fullScreencall();

    setContentView(R.layout.activity_capture);

    mCamera = getCameraInstance();

    // Create our Preview view and set it as the content of our activity.
    mPreview = new CameraPreview(this, mCamera);
    FrameLayout preview = (FrameLayout) findViewById(R.id.camera);
    preview.addView(mPreview, 0);
    mButtonCapture = (Button) findViewById(R.id.btnCapturePhoto);
    mButtonCapture.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ImageSaverThread ist = new ImageSaverThread(mCamera);
            ist.start();
        }
    });
}

To me, this seems not to be so much work, and I cannot find anything that I could separate into another thread. Taking a photo and saving it is already done in a thread.

Any idea what I am missing here?

EDIT

ImageSaverThread.java

public class ImageSaverThread extends Thread {

    private Camera mCamera;
    public static final int MEDIA_TYPE_IMAGE = 1;

    public ImageSaverThread(Camera camera){
        mCamera = camera;
    }

    public void run(){
        PictureCallback mPicture = new PictureCallback() {

            @Override
            public void onPictureTaken(byte[] data, Camera camera) {

                File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
                if (pictureFile == null){
                    return;
                }

                try {
                    FileOutputStream fos = new FileOutputStream(pictureFile);
                    fos.write(data);
                    fos.flush();
                    fos.close();
                } catch (FileNotFoundException e) {
                    Log.d("FAIL", "File not found: " + e.getMessage());
                } catch (IOException e) {
                    Log.d("FAIL", "Error accessing file: " + e.getMessage());
                }
            }
        };
        mCamera.takePicture(null, null, mPicture);
    }

    private static File getOutputMediaFile(int type){
        // To be safe, you should check that the SDCard is mounted
        // using Environment.getExternalStorageState() before doing this.

        File mediaStorageDir = new File(String.valueOf(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)));
        // This location works best if you want the created images to be shared
        // between applications and persist after your app has been uninstalled.

        // Create the storage directory if it does not exist
        if (! mediaStorageDir.exists()){
            if (! mediaStorageDir.mkdirs()){
                Log.d("MyCameraApp", "failed to create directory");
                return null;
            }
        }
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File mediaFile;
        if (type == MEDIA_TYPE_IMAGE){
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "IMG_"+ timeStamp + ".jpg");
        } else {
            return null;
        }

        return mediaFile;
    }
}
PKlumpp
  • 4,913
  • 8
  • 36
  • 64

1 Answers1

1

getCameraInstance can be a long running process. You may need to run it on another thread. The documentation suggests doing that.

See the answer posted here for more about that: Android camera: Threads? Which should do what

EDIT:

When someone clicks on the button, you might have a different issue. Your static method is not created until the ImageSaverThread object is instantiated. Checking for the file, etc. might take some time as a static method, so you may want to change that to a non-static method.

For more, see this post. Static Initialization Blocks

Also, this is a "warning" - not an "error". 35 frames is not much, and the frame count will vary depending on available memory, platform and other priority threads that might be running.

Community
  • 1
  • 1
Jim
  • 10,172
  • 1
  • 27
  • 36