2011-12-11 129 views
1

我想知道是否有人能告訴我爲什麼我的照片被保存爲0kb的大小,這意味着他們是空的?...我檢查了我的SD卡,他們正在被保存但沒有任何東西在他們中......繼承人代碼攝像頭圖像被保存到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); 

     } 







}; 
} 

感謝斯特凡

回答

0

兩件事情:

finalBitmap永遠不會被初始化,所以我會假設你每一次捕捉空指針錯誤在try catch塊在jpegCallback 。在回調中傳遞給你的字節數組也沒有做任何事情,這意味着你沒有做任何事情來保存圖像。試着做下面的onPictureTaken的開始jpegCallback:

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