Image Capture and save in Android 1.5

Tutorials with advanced 'difficulty' and more Lines of Code.

Image Capture and save in Android 1.5

Postby maneesh_17 » Tue Sep 08, 2009 12:39 am

Hi,

I am able to capture the image with the following code with in my application but I don't know how to save it in to mobile(Gallery) ? I have gone through the different codes on net but I think they belongs to Android 1.0.



import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.os.Bundle;
import android.provider.MediaStore.Images;
import android.provider.MediaStore.Video;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.KeyEvent;
import android.view.Window;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;

import android.util.Log;

public class CameraApiTest extends Activity implements SurfaceHolder.Callback
{
private static final String TAG = "CameraApiTest";
Camera mCamera;
boolean mPreviewRunning = false;
Context context;
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
requestWindowFeature(Window.FEATURE_NO_TITLE);
Log.e(TAG, "onCreate");

getWindow().setFormat(PixelFormat.TRANSLUCENT);

setContentView(R.layout.camera_api_test);
mSurfaceView = (SurfaceView)findViewById(R.id.surface);

mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

public boolean onCreateOptionsMenu(android.view.Menu menu) {
MenuItem item = menu.add(0, 0, 0, "goto gallery");
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Uri target = Uri.parse("content://media/external/images/media");
Intent intent = new Intent(Intent.ACTION_VIEW, target);
startActivity(intent);
return true;
}
});
return true;
}

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

Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera c) {
Log.e(TAG, "PICTURE CALLBACK: data.length = " + data.length);
mCamera.startPreview();
}
};



public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK) {
return super.onKeyDown(keyCode, event);
}

if (keyCode == KeyEvent.KEYCODE_SPACE) {
mCamera.takePicture(shutterCallback, rawCallback,mPictureCallback);
return true;
}

return false;
}

ShutterCallback shutterCallback = new ShutterCallback() {
public void onShutter() {
// TODO Do something when the shutter closes.
}
};

PictureCallback rawCallback = new PictureCallback() {
public void onPictureTaken(byte[] _data, Camera _camera) {
// TODO Do something with the image RAW data.
}
};

protected void onResume()
{
Log.e(TAG, "onResume");
super.onResume();
}

protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
}

protected void onStop()
{
Log.e(TAG, "onStop");
super.onStop();
}

public void surfaceCreated(SurfaceHolder holder)
{
Log.e(TAG, "surfaceCreated");
mCamera = Camera.open();
//mCamera.startPreview();
}

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

// XXX stopPreview() will crash if preview is not running
if (mPreviewRunning) {
mCamera.stopPreview();
}

Camera.Parameters p = mCamera.getParameters();
p.setPreviewSize(w, h);
mCamera.setParameters(p);
try{
mCamera.setPreviewDisplay(holder);
}catch(Exception e)
{
new AlertDialog.Builder(context).setTitle("Error")
.setMessage(e.getMessage())
.setPositiveButton("OK", null).show();
}
mCamera.startPreview();
mPreviewRunning = true;
}

public void surfaceDestroyed(SurfaceHolder holder)
{
Log.e(TAG, "surfaceDestroyed");
mCamera.stopPreview();
mPreviewRunning = false;
mCamera.release();
}

private SurfaceView mSurfaceView;
private SurfaceHolder mSurfaceHolder;
}
maneesh_17
Junior Developer
Junior Developer
 
Posts: 17
Joined: Tue Sep 08, 2009 12:27 am
Location: Gurgaon,India

Top

Postby matany » Sat Jan 16, 2010 5:14 pm

Hi maneesh_17,

Thanks for sharing the code, did you ever find your solution and what was it? I need to do the samething and I am having the same problem.

Thanks alot.

Mat
matany
Experienced Developer
Experienced Developer
 
Posts: 66
Joined: Tue Dec 08, 2009 9:29 pm

Postby asolanki » Fri Mar 26, 2010 9:52 am

Hi!!!

we can pass android.provider.MediaStore.EXTRA_OUTPUT with the intent for Camera like this

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. mImageCaptureUri = Uri.fromFile(new File("/sdcard/camera.jpg"));
  3.  
  4.        
  5.  
  6. intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


by doing this we can save file in /sdcard/camera.jpg
asolanki
Senior Developer
Senior Developer
 
Posts: 151
Joined: Thu Jan 08, 2009 7:32 am

Top

Return to Advanced Tutorials

Who is online

Users browsing this forum: No registered users and 4 guests