我想知道是否有任何人可以告诉我为什么我的照片被保存的大小为0kb意味着它们是空的?...我检查了我的SD卡并且他们正在被保存但是没有任何内容......代码
public class CameraAPI extends Activity implements SurfaceHolder.Callback{
public Camera camera;
MediaRecorder mediaRecorder;
CBDataBaseHelper RH;
TextView RecipeID;
Bitmap finalBitmap;
public void onCreate (Bundle savedInstanceState){
boolean diditwork;
try{
super.onCreate(savedInstanceState);
setContentView(R.layout.camera);
RecipeID = (TextView)findViewById(R.id.Rid2);
String RowID;
Bundle extras = getIntent().getExtras();
RowID = extras.getString("SELECTED2");
RecipeID.setText(RowID);
SurfaceView surface = (SurfaceView)findViewById(R.id.acccam);
SurfaceHolder holder = surface.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}catch(Exception e){
diditwork = false;
String error = e.toString();
Dialog d = new Dialog(this);
d.setTitle("darn");
TextView tv = new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();
}}
public void takePhoto(View view){
//String ID = RecipeID.getText().toString();
//Long LID = Long.parseLong(ID);
boolean diditwork;
try{
takePicture();
}catch(Exception e){
diditwork = false;
String error = e.toString();
Dialog d = new Dialog(this);
d.setTitle("darn");
TextView tv = new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();
}
//String path = "/sdcard/Image.jpg";
//RH.updateRecipe3(LID,path);
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
if (mediaRecorder == null){
try{
camera = camera.open();
camera.setPreviewDisplay(holder);
camera.startPreview();
}catch (IOException e){
Log.d("CAMERA", e.getMessage());
}
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
camera.stopPreview();
camera.release();
}
public void takePicture(){
boolean diditwork;
try{
camera.takePicture(shutterCallback, rawCallback, jpegCallback);
}catch(Exception e){
diditwork = false;
String error = e.toString();
Dialog d = new Dialog(this);
d.setTitle("darn");
TextView tv = new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();
}
}
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){
// FileOutputStream outStream = null;
boolean diditwork;
File myDir=new File("/sdcard");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "/Image"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
RH = new CBDataBaseHelper(CameraAPI.this);
RH.open();
String ID = RecipeID.getText().toString();
Long RID = Long.parseLong(ID);
RH.updateRecipe3(RID, myDir +fname);
RH.close();
//String ID = RecipeID.getText().toString();
//Long RID = Long.parseLong(ID);
//RH.updateRecipe3(RID, myDir + fname);
}
};
}
感谢Stefan
答案 0 :(得分:0)
两件事:
finalBitmap永远不会被初始化,所以我假设你每次都在jpegCallback的try catch块中捕获一个空指针错误。您也没有对回调中传递给您的字节数组做任何事情,这意味着您没有做任何事情来保存图像。尝试在jpegCallback中的onPictureTaken开头执行以下操作:
finalBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
请参阅BitmapFactory了解详情
如果您的目标是简单地允许用户拍照并且您并不关心细节,那么最简单的方法是使用startActivityForResult并让本机相机应用程序处理图片并创建文件。如果您需要使用之后创建的图像,则可以传入Uri,以便知道图像的保存位置。这与使用Android的MediaStore有额外的好处。
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//Uri is parcelable so you can save this to a bundle in onSaveInstanceState
//and retrieve it in onResume
photoUri = getActivity().getContentResolver().insert(EXTERNAL_CONTENT_URI, new ContentValues());
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
// Lanuch the activity
startActivityForResult(intent, TAKE_PHOTO);
然后,您可以通过实现onActivityResult()来处理结果。见startActivityForResult