How to store byte[] from Android Camera onPictureTaken method within application for later use
- by Kiel Wood
I am writing a larger Android application and I use the camera within the app. All I want to do with the camera is have the user take a picture, then start a new activity to show that image and allow the user to decide if they want to keep the image or not. I am having the hardest time figuring out how to simply store the byte[] data from the onPictureTaken method so that I can display it to the user in the next activity. I have tried many different routes and none of them have worked. The last thing I tried was creating a globalsettings class that extends the Application class and creating a byte[] field within it to store the byte[] from the camera so that I could use it within another activity, but my global variable is still not getting set. My CameraActivity code is shown below:
public class CameraActivity extends Activity
{
CameraPreview Preview;
Intent intent;
byte[] image;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.cameralayout);
Preview = new CameraPreview(this);
((FrameLayout)findViewById(R.id.uxfmlayPreview)).addView(Preview);
intent = new Intent(this, PostCaptureActivity.class);
}
public void uxbtnCaptureSnap_Click(View v)
{
Preview.DeviceCamera.setPreviewCallback(null);
Preview.DeviceCamera.takePicture(shutterCallback, rawCallback, jpegCallback);
startActivity(intent);
finish();
}
public void uxbtnCaptureExit_Click(View v)
{
Intent i = new Intent(this, ExploreMenuActivity.class);
setResult(RESULT_OK);
startActivity(i);
finish();
}
ShutterCallback shutterCallback = new ShutterCallback()
{
public void onShutter() {}
};
PictureCallback rawCallback = new PictureCallback()
{
public void onPictureTaken(byte[] data, Camera camera) {}
};
PictureCallback jpegCallback = new PictureCallback()
{
public void onPictureTaken(byte[] data, Camera camera)
{
((GlobalSettings)getApplication()).setGlobalImage(data);
camera.release();
camera = null;
}
};
}
Here is my code from my PostCaptureActivity onCreate() method where I attempt to convert and set the image as the source for an imageview:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.postcapturelayout);
SnapShot = ((ImageView)findViewById(R.id.uximgSnapshot));
if(((GlobalSettings)this.getApplication()).getGlobalImage() != null)
{
Bitmap b = BitmapFactory.decodeByteArray(((GlobalSettings)this.getApplication()).getGlobalImage(), 0, ((GlobalSettings)this.getApplication()).getGlobalImage().length);
SnapShot.setImageBitmap(b);
}
else
{
Toast.makeText(this, "Oops! Picture cannot be saved", Toast.LENGTH_SHORT).show();
}
}