2014-06-21 18 views
5

Arka planda görüntü alan hizmetimin içinde ScheduledExecutorService çalıştırıyorum. Sadece bir kez çalışan ScheduledExecutorService, işlev geçtikten sonra sadece bir resim çekmek anlamına gelir. Hiç hata gösterilmiyor. Aşağıdaki herhangi bir hata almıyorum çünkü benim ServiceScheduledExecutorService Service içinde yalnızca bir kez çalıştırın

package com.anawaz.spy; 

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.util.concurrent.Executors; 
import java.util.concurrent.ScheduledExecutorService; 
import java.util.concurrent.ScheduledFuture; 
import java.util.concurrent.TimeUnit; 

import android.app.Service; 
import android.content.Intent; 
import android.hardware.Camera; 
import android.hardware.Camera.PictureCallback; 
import android.hardware.Camera.ShutterCallback; 
import android.os.IBinder; 
import android.util.Log; 

public class CamService extends Service { 

    private static final String TAG = "TAG"; 

    private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); 

    Camera camera; 
    ScheduledFuture beeperHandle; 

    /* Service Life cycle Overrides */ 
    @Override 
    public void onCreate() { 
     super.onCreate(); 
     camera = Camera.open(); 
     try { 
      camera.setPreviewDisplay(null); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     //takePicsPeriodically(MainActivity.getSpInt(MainActivity.SP_Period)); 
     takePicsPeriodically(5); 
    } 

    @Override 
    public void onDestroy() { 
     stopPics(); 
     super.onDestroy(); 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    // @Override 
    // public IBinder onBind(Intent arg0) { 
    // return myRemoteServiceStub; 
    // } 
    // /* ----------------------------------- */ 
    // 
    // private IMyRemoteService.Stub myRemoteServiceStub = new 
    // IMyRemoteService.Stub() { 
    // 
    // /* Basic Service Methods */ 
    // public boolean isCollecting() { 
    // return (beeperHandle != null); 
    // } 
    // /* ------------------------- */ 
    // }; 

    public void takePicsPeriodically(long period) { 
     final Runnable beeper = new Runnable() { 
      public void run() { 
       Log.d("TAG", "New Picture Taken"); 
       camera.takePicture(shutterCallback, rawCallback, jpegCallback); 
      } 
     }; 
     camera.startPreview(); 
     beeperHandle = scheduler.scheduleAtFixedRate(beeper, period, period,TimeUnit.SECONDS); 
    } 

    public void stopPics() { 
     beeperHandle.cancel(true); 
     beeperHandle = null; 
     camera.stopPreview(); 
     camera.release(); 
     camera = null; 
    } 

    /* Camera Call backs */ 
    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 j peg picture */ 
    PictureCallback jpegCallback = new PictureCallback() { 
     public void onPictureTaken(byte[] data, Camera camera) { 
      FileOutputStream outStream = null; 
      try { 
       // write to local sand box file system 
       // outStream = 
       // CameraDemo.this.openFileOutput(String.format("%d.jpg", 
       // System.currentTimeMillis()), 0); 
       // Or write to s d card 
       File mFolder; 
       mFolder = new File("/sdcard/Spy/"); 
       if (!mFolder.exists()) { 
        mFolder.mkdir(); 
       } 
       outStream = new FileOutputStream(String.format(
         "/sdcard/Spy/Image_%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"); 
     } 
    }; 

} 

Hayır LogCat ait kodudur. Servis resim çekildikten sonra da çalışıyor. Ne yapıyorum yanlış?

cevap

9

Bu iletiyi okuduktan sonra http://code.nomad-labs.com/2011/12/09/mother-fk-the-scheduledexecutorservice/try catch bloğunu denedim. Ve bu camera.takePicture(shutterCallback, rawCallback, jpegCallback); istisna atıyor bulundu. Bu benim hatamdan kaynaklanıyordu. İlk fotoğrafı çektikten sonra önizlemeyi başlatmıyordum. Bu cevaba bakın. https://stackoverflow.com/a/21728454/1770916

Servisimin Güncel Çalışma Kodu aşağıdadır.

package com.anawaz.spy; 

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.util.concurrent.Executors; 
import java.util.concurrent.ScheduledExecutorService; 
import java.util.concurrent.ScheduledFuture; 
import java.util.concurrent.TimeUnit; 

import android.app.Service; 
import android.content.Intent; 
import android.hardware.Camera; 
import android.hardware.Camera.PictureCallback; 
import android.hardware.Camera.ShutterCallback; 
import android.os.IBinder; 
import android.util.Log; 

public class CamService extends Service { 

    private static final String TAG = "TAG"; 

    private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); 

    Camera camera; 
    ScheduledFuture beeperHandle; 

    /* Service Life cycle Overrides */ 
    @Override 
    public void onCreate() { 
     super.onCreate(); 
     camera = Camera.open(); 
     try { 
      camera.setPreviewDisplay(null); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     File mFolder = new File("/sdcard/Spy/"); 
     if (!mFolder.exists()) { 
      mFolder.mkdir(); 
     } 
     takePicsPeriodically(MainActivity.getSpInt(MainActivity.SP_Period)); 
     //takePicsPeriodically(5); 
    } 

    @Override 
    public void onDestroy() { 
     stopPics(); 
     super.onDestroy(); 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    public void takePicsPeriodically(long period) { 
     camera.startPreview(); 
     beeperHandle = scheduler.scheduleAtFixedRate(beeper,period, period,TimeUnit.SECONDS); 
    } 

    final Runnable beeper = new Runnable() { 
     public void run() { 
      Log.d("TAG", "New Picture Taken"); 
      try { 
       camera.startPreview(); 
       camera.takePicture(shutterCallback, rawCallback, jpegCallback); 
      }catch (Exception e) { 
       Log.e("TAG","error in executing: It will no longer be run!: "+e.getMessage()); 
       e.printStackTrace(); 
      } 
     } 
    }; 
    public void stopPics() { 
     beeperHandle.cancel(true); 
     beeperHandle = null; 
     camera.stopPreview(); 
     camera.release(); 
     camera = null; 
    } 

    /* Camera Call backs */ 
    final ShutterCallback shutterCallback = new ShutterCallback() { 
     public void onShutter() { 
      Log.d(TAG, "onShutter'd"); 
     } 
    }; 

    /** Handles data for raw picture */ 
    final PictureCallback rawCallback = new PictureCallback() { 
     public void onPictureTaken(byte[] data, Camera camera) { 
      Log.d(TAG, "onPictureTaken - raw"); 
     } 
    }; 

    /** Handles data for j peg picture */ 
    final PictureCallback jpegCallback = new PictureCallback() { 
     public void onPictureTaken(byte[] data, Camera camera) { 
      FileOutputStream outStream = null; 
      try { 
       // write to local sand box file system 
       // outStream = 
       // CameraDemo.this.openFileOutput(String.format("%d.jpg", 
       // System.currentTimeMillis()), 0); 
       // Or write to s d card 

       outStream = new FileOutputStream(String.format(
         "/sdcard/Spy/Image_%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"); 
     } 
    }; 

} 
+0

Başlıklar için teşekkürler :) –