2014-03-05 44 views
0

我有一個IntentService接收來自使用GCM的服務器的消息,除了當我嘗試拍攝使用android.hardware.Camera的圖片時,一切都正常工作,InentService在調用onPictureTaken之前被銷燬! ,那麼我應該怎麼做才能在InentService停止之前保存捕獲的照片?如何在IntentService中使用Camera API?

public class GcmIntentService extends IntentService { 
    String function; 

    public GcmIntentService() { 
    super("GcmIntentService"); 

} 
    @Override 
    protected void onHandleIntent(Intent intent) { 
    Bundle extras = intent.getExtras(); 
    String msg = intent.getStringExtra("message"); 
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this); 
    String messageType = gcm.getMessageType(intent); 

    if (!extras.isEmpty()) { 
       if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) { 
       System.out.println("message received"); 

        try { 
       json = new JSONObject(msg); 
       function = json.getString("function"); 

      } catch (JSONException e) { 

       e.printStackTrace(); 
      } 
        if (function.equals("capture")) { 
       System.out 
         .println("capture entereeeeed ====================="); 

       Capture cap = new Capture(getApplicationContext()); 
       cap.captureNow(); 


      } 

     } 
    } 
    GcmBroadcastReceiver.completeWakefulIntent(intent); 
} 




    @Override 
public void onDestroy() { 

    if (Capture.camera != null) { 
     System.out.println("camera released"); 
     Capture.camera.release(); 
    } 

    super.onDestroy(); 
} 

}

捕獲類,從vogella教程http://www.vogella.com/tutorials/AndroidCamera/article.html

public class Capture { 
Context captureContext = null; 
public static Camera camera; 
private int cameraId = 0; 
final static String DEBUG_TAG = "Capture Class"; 

public Capture(Context context) { 

    captureContext = context; 
    // do we have a camera? 
    if (!captureContext.getPackageManager().hasSystemFeature(
      PackageManager.FEATURE_CAMERA)) { 

     System.out.println("no camera found on this device"); 
    } else { 
     cameraId = findFrontFacingCamera(); 
     if (cameraId < 0) { 

      System.out.println("no front camera found"); 
     } else { 
      camera = Camera.open(cameraId); 
     } 
    } 
} 

public void captureNow() { 
    System.out.println("capture now entered"); 
    camera.takePicture(null, null, new PhotoHandler(captureContext)); 
} 

public int findFrontFacingCamera() { 
    int cameraId = -1; 
    // Search for the front facing camera 
    int numberOfCameras = Camera.getNumberOfCameras(); 
    for (int i = 0; i < numberOfCameras; i++) { 
     CameraInfo info = new CameraInfo(); 
     Camera.getCameraInfo(i, info); 
     if (info.facing == CameraInfo.CAMERA_FACING_FRONT) { 
      Log.d(DEBUG_TAG, "Camera found"); 
      cameraId = i; 
      break; 
     } 
    } 
    return cameraId; 
} 

}

的PhotoHandler類

public class PhotoHandler implements PictureCallback { 
private final Context context; 
public static String filename; 

public PhotoHandler(Context context) { 
    System.out.println("enetered photoHandler"); 
    this.context = context; 

} 

@Override 
public void onPictureTaken(byte[] data, android.hardware.Camera camera) { 
    System.out.println("entered onPicture taken"); 

    File pictureFileDir = getDir(); 
    System.out.println("file directory = " + pictureFileDir); 

    if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) { 

     Log.d(Capture.DEBUG_TAG, "Can't create directory to save image."); 

     return; 

    } 

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss"); 
    String date = dateFormat.format(new Date()); 
    String photoFile = "Picture_" + date + ".jpg"; 
    filename = pictureFileDir.getPath() + File.separator + photoFile; 

    File pictureFile = new File(filename); 


    try { 
     FileOutputStream fos = new FileOutputStream(pictureFile); 
     fos.write(data); 
     fos.close(); 

     System.out.println("New Image saved: " + photoFile); 
     Capture.camera.release(); 


    } catch (Exception error) { 
     Log.d(Capture.DEBUG_TAG, 
       "File" + filename + "not saved: " + error.getMessage()); 

     System.out.println("image could not be saved"); 
    } 


} 

private File getDir() { 
    File sdDir = Environment 
      .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); 
    return new File(sdDir, "blah"); 
} 

}

回答

0

答案很簡單隻要在AsyncTask中做相機的東西。可能會工作。

@Override 
    protected void onHandleIntent(Intent intent) { 
    Bundle extras = intent.getExtras(); 
    String msg = intent.getStringExtra("message"); 
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this); 
    String messageType = gcm.getMessageType(intent); 

     if (!extras.isEmpty()) { 
      if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) { 
      System.out.println("message received"); 

       try { 
      json = new JSONObject(msg); 
      function = json.getString("function"); 

     } catch (JSONException e) { 

      e.printStackTrace(); 
     } 
       if (function.equals("capture")) { 
      System.out.println("capture entereeeeed ====================="); 
      new AsyncTask<Void, Void, String>() { 
       @Override 
        protected String doInBackground(Void...params) { 
        Capture cap = new Capture(getApplicationContext()); 
         cap.captureNow(); 
        String result = "ok"; 
      return result; 
        } 
        @Override 
        protected void onPostExecute(String result) { 




        } 

       }.execute(); 
     } 

    } 
} 
相關問題