0

I built together a simple camera app (from various sources in the internet, using SurfaceHolder) as a prototype to be integrated later on with a bigger project.

The app works really well. It doesn't crash and saves the images properly.

I started integrating it with the bigger project. It is now started from another activity (instead of being the main activity). It still shows the expected camera view and does not crash.

However, it won't save any of the pictures taken.

The code of the independent camera app and the integrated camera activity are identical.

I tested the integrated camera code without calling finish() or starting other activities (just to make sure that it outputs the image properly).

But it still doesn't work.

Anyone encountered this before? Thanks.

This is the source code for the camera activity:

public class CameraViewActivity extends Activity implements
    SurfaceHolder.Callback, OnClickListener {

private static final String SD_CARD_LOC = "/sdcard/";
private static final String FILE_NAME = "image";
private static final String JPEG_FORMAT = ".jpg";
private static final String TAG = "CameraViewActivity";

private Context context = this;
private SurfaceView surfaceView;
private SurfaceHolder surfaceHolder;

Camera camera;
boolean isPreviewRunning = false;

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    Log.e(TAG, "onCreate");

    //Bundle extras = getIntent().getExtras();

    getWindow().setFormat(PixelFormat.TRANSLUCENT);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.camera_view_layout);
    surfaceView = (SurfaceView) findViewById(R.id.surface_camera);
    surfaceView.setOnClickListener(this);
    surfaceHolder = surfaceView.getHolder();
    surfaceHolder.addCallback(this);
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
}

Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
    public void onPictureTaken(byte[] imageByteData, Camera c) {
        if (imageByteData != null) {

            Intent intent = new Intent(CameraViewActivity.this,SharePhotoActivity.class);

            if(saveByteImage(context, imageByteData, 50)){
                camera.startPreview();
                startActivity(intent);
                finish();
            }
        }
    }
};

@Override
public void onResume() {
    Log.e(TAG, "onResume");
    super.onResume();
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
}

@Override
public void onStop() {
    Log.e(TAG, "onStop");
    super.onStop();
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    Log.e(TAG, "surfaceCreated");
    camera = Camera.open();
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    Log.e(TAG, "surfaceChanged");

    if (isPreviewRunning) {
        camera.stopPreview();
    }

    Camera.Parameters parameters = camera.getParameters();
    int height = parameters.getPreviewSize().height;
    int width = parameters.getPreviewSize().width;

    Display display = ((WindowManager) getSystemService(WINDOW_SERVICE))
            .getDefaultDisplay();

    switch (display.getRotation()) {
    case Surface.ROTATION_0: {
        parameters.setPreviewSize(width, height);
        camera.setDisplayOrientation(90);
    }
        break;
    case Surface.ROTATION_180: {
        parameters.setPreviewSize(width, height);
    }
        break;
    case Surface.ROTATION_270: {
        parameters.setPreviewSize(width, height);
        camera.setDisplayOrientation(180);
    }
        break;
    case Surface.ROTATION_90: {
        parameters.setPreviewSize(width, height);
    }
        break;
    }

    camera.setParameters(parameters);
    try {
        camera.setPreviewDisplay(holder);
        camera.startPreview();
        isPreviewRunning = true;
    } catch (IOException e) {
        e.printStackTrace();
    }
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    Log.e(TAG, "surfaceDestroyed");
    camera.stopPreview();
    isPreviewRunning = false;
    camera.release();
}

@Override
public void onClick(View arg0) {
    camera.takePicture(null, mPictureCallback, mPictureCallback);
}

public static boolean saveByteImage(Context context, byte[] imageByteData, int quality) {

    StringBuffer fileUri = new StringBuffer();
    fileUri.append(SD_CARD_LOC);
    fileUri.append(FILE_NAME);
    fileUri.append("-");
    fileUri.append(System.currentTimeMillis());
    fileUri.append(JPEG_FORMAT);

    try {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 5;
        Bitmap bitmapImage = BitmapFactory.decodeByteArray(imageByteData, 0, imageByteData.length, options);
        FileOutputStream fos = new FileOutputStream(fileUri.toString());
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        bitmapImage.compress(CompressFormat.JPEG, quality, bos);

        bos.flush();
        bos.close();        
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return true;
}

}

Update: I created a new android project with only two activities: a main one and the camera view activity. The main activity simply launches the camera view activity via a button click. And still won't save any images.

1 Answers1

0

I have fixed it.

Apparently the prototype I made was using an android api version older than version 4.

And does not require the following permission:

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

I just added it and it works well now.

Thanks.