Reputation: 11
I'm trying to take a picture and when the user clicks the button, I want to save the picture to the internal storage. I will be testing on a tablet which does not have an sd card. I'm also new to taking pictures on android and the web has been no help with trying to find what specific code I need to write it to somewhere other than an sd card. Here is what my camera.java looks like.
package com.test;
import com.test.R;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
public class camera extends Activity
{
private static final String TAG = "CameraDemo";
Camera camera;
Preview preview;
Button buttonClick;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera);
preview = new Preview(this);
((FrameLayout) findViewById(R.id.preview)).addView(preview);
buttonClick = (Button) findViewById(R.id.buttonClick);
buttonClick.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);
}
});
Log.d(TAG, "onCreate'd");
}
ShutterCallback shutterCallback = new ShutterCallback() {
public void onShutter() {
Log.d(TAG, "onShutter'd");
}
};
/** Handles data for raw picture */
PictureCallback rawCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
Log.d(TAG, "onPictureTaken - raw");
}
};
/** Handles data for jpeg picture */
PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
FileOutputStream outStream = null;
try {
// write to local sandbox file system
outStream = camera.this.openFileOutput(String.format("%d.jpg", System.currentTimeMillis()), 0);
// Or write to sdcard
//outStream = new FileOutputStream(String.format("/sdcard/%d.jpg", System.currentTimeMillis()));
outStream.write(data);
outStream.close();
Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
Log.d(TAG, "onPictureTaken - jpeg");
}
};
}
Upvotes: 1
Views: 3262
Reputation: 31466
You can see this Picture Demo sample from CommonsAware's Github page, it might be useful to you.
Upvotes: 0
Reputation: 3330
It's not really clear what kind of problem you're getting with that code. The way you're opening the file seems fine, so maybe it's behaving in another way that's incorrect?
This page in the Android document has details about using the different kinds of storage available to your application: http://developer.android.com/guide/topics/data/data-storage.html
In particular, the section about saving files that should be shared might be useful to your situation. It references "external storage" but many devices simulate external storage using internal storage, so as a developer you don't need to worry about it.
If you want the picture to be viewable in the system's Gallery app, you can add it to media scanning using MediaScannerConnection.scanFile. That may not work, though, if you are storing files in your app's private storage with openFileOutput
.
Upvotes: 2