我想知道是否any1可以告诉我为什么我的照片以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);

        }







};
}

谢谢斯蒂芬

===============>>#1 票数: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

  ask by aspiringCoder translate from so

未解决问题?本站智能推荐: