Bonjour,

Je suis en train de développer une application Android qui prend des photos toutes les n secondes mais je n'arrive pas à afficher de preview de ma caméra sur mon téléphone.

Le preview marchait parfaitement lorsque je générais les prises de vues après l'appui sur un bouton mais plus depuis que c'est automatisé.

L'application plante tout simplement lorsque j'essaye d'afficher une preview.

J'ai essayer de placer mon code à plusieurs endroits (onResume; onCreate...) mais même résultat à chaque fois. J'ai aussi essayé de faire un rafraichissement de ma preview après chaque prise de vue, mais même soucis au final.

Logcat:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
03-29 10:51:16.575: D/AndroidRuntime(660): Shutting down VM   
    03-29 10:51:16.575: W/dalvikvm(660): threadid=1: thread exiting with uncaught exception         (group=0x40c571f8)   
    03-29 10:51:16.575: E/AndroidRuntime(660): FATAL EXCEPTION: main   
    03-29 10:51:16.575: E/AndroidRuntime(660): java.lang.RuntimeException: Unable to start activity
MainActivity
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
class ImageSender implements Runnable{   
 
     final private int limit = 5;   
     int current = 0;   
     boolean alive=true;   
 
     synchronized void received(){   
      current--;   
      Log.d("PPK","notifay");   
      this.notify();   
     }   
 
 
     synchronized void reset(){   
      current=0;   
      alive=false;   
      this.notify();   
     }   
 
     synchronized void alive(){   
      alive=true;   
     }   
 
  @Override   
  public void run() {   
   Log.d("ImageSender", "starting thread");   
   try{   
    while(true){   
     Log.d("ImageSender", "looping");   
     while(alive && current<limit){   
      Log.d("ImageSender", "looping OK");   
      Log.d("PPK","looping  with current" + current);   
      current++;   
 
       mCamera = getCameraInstance();   
 
            try {   
             mCamera.takePicture(null, null, null, mPicture);   
            } catch(Throwable e) {   
             Log.d("ERROR", "Throwable "+e.getMessage());   
             Log.d("ERROR", "Throwable2 "+e.toString());   
            }   
 
      try {   
       Thread.sleep(1050); //50   
      } catch (InterruptedException e) {   
       e.printStackTrace();   
      }   
     }   
     try {   
      synchronized (this) {   
       this.wait(1020);    
      }   
     } catch (InterruptedException e) {   
      e.printStackTrace();   
     }   
    }    
 
   }catch (Throwable t) {   
    // TODO: handle exception    
    Log.d("EX", "expection out of memory ?"+t );   
    t.printStackTrace();   
    clientState = new MyWebSocketListener();   
   }   
  }   
    }   
 
 
 private String TAG = "HTTPCamera";   
 
 private CameraSurfaceView cameraSurfaceView;   
 
 
 @Override   
 public void onCreate(Bundle savedInstanceState) {   
  super.onCreate(savedInstanceState);   
 
 
  clientState = new MyWebSocketListener();   
 
        requestWindowFeature(Window.FEATURE_NO_TITLE);   
        Window win = getWindow();   
  win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);     
        win.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,   
  WindowManager.LayoutParams.FLAG_FULLSCREEN);    
 
 
 
        Toast.makeText(getApplicationContext(), "hello", Toast.LENGTH_SHORT).show();   
 
 }   
 
 private Camera mCamera;   
 private PicPreview mPreview;   
 private final static String TAG2 = "MainActivity";   
 private int mCount;   
 private FrameLayout preview;   
 
 
 //private SocketIO client;   
 
 
 @Override   
 protected void onResume() {   
  super.onResume();   
 
  setContentView(R.layout.activity_main);   
 
 
 
  // Create an instance of Camera   
        mCamera = getCameraInstance();   
 
 
        Log.d(TAG2, "setting client done");   
 
        mPreview = new PicPreview(this, mCamera);   
        preview = (FrameLayout) findViewById(R.id.camera_preview);   
        preview.addView(cameraSurfaceView);   
 
        Log.d(TAG, "setting preview done");   
 }   
 
 public void showToast(final String toast)   
 {   
     runOnUiThread(new Runnable() {   
         public void run()   
         {   
             Toast.makeText(MainActivity.this, toast, Toast.LENGTH_LONG).show();   
         }   
     });   
 }   
 
 private PictureCallback mPicture = new PictureCallback() {   
 
     @Override   
     public void onPictureTaken(byte[] byteData, Camera camera) {   
      Log.d(TAG2, "event: picture beginning");   
      Log.d(TAG2, "byte " + byteData);   
 
      Log.d(TAG2, "client: " + clientState);   
 
      clientState.send(byteData);   
 
      Log.d(TAG2, "event: picture sent");   
     }   
 };   
 
 @Override   
 public void onPause() {   
     super.onPause();  // Always call the superclass method first   
 
     // Release the Camera because we don't need it when paused   
     // and other activities might need to use it.   
     if (mCamera != null) {   
      mCamera.stopPreview();   
         mCamera.release();   
         mCamera = null;   
     }   
 }   
 
 @Override   
 protected void onStop() {   
     super.onStop();  // Always call the superclass method first   
 
 }   
 
 @Override   
 protected void onStart() {   
     super.onStart();  // Always call the superclass method first   
 
 }   
 
 @Override   
 protected void onRestart() {   
     super.onRestart();  // Always call the superclass method first   
 
     // Activity being restarted from stopped state       
 }   
 
 public Camera getCameraInstance(){   
  if (mCamera != null) {   
   return mCamera;   
  }   
 
  int cameraId=0;   
     int cameraCount = 0;   
     Camera.CameraInfo cameraInfo = new Camera.CameraInfo();   
        cameraCount = Camera.getNumberOfCameras();   
        for ( int camIdx = 0; camIdx < cameraCount; camIdx++ ) {   
            Camera.getCameraInfo( camIdx, cameraInfo );   
            if ( cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {   
                try {   
                 mCamera = Camera.open( camIdx );   
                 Log.d(TAG2, "cameraId: " + cameraId);   
                 return mCamera;    
                } catch (RuntimeException e) {   
                    Log.e(TAG2, "Camera failed to open: " + e.getLocalizedMessage());   
                }   
            }   
        }   
        return null;   
 }   
 
 @Override   
 public boolean onCreateOptionsMenu(Menu menu) {   
  // Inflate the menu; this adds items to the action bar if it is present.   
  getMenuInflater().inflate(R.menu.activity_main, menu);   
  return true;   
 }   
    }
Activity_main.xml

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
 <?xml version="1.0" encoding="utf-8"?>   
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
        android:orientation="vertical"   
        android:layout_width="fill_parent"   
        android:layout_height="fill_parent"   
        >   
      <FrameLayout   
         android:id="@+id/camera_preview"   
         android:layout_width="fill_parent"   
         android:layout_height="fill_parent"   
         android:layout_weight="1"   
         />   
    </LinearLayout>

Manifest:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 <?xml version="1.0" encoding="utf-8"?>   
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"   
    package="com.example.photopic"   
    android:versionCode="1"   
    android:versionName="1.0"   
    >   
 
    <uses-sdk   
        android:minSdkVersion="14"   
        android:targetSdkVersion="17" />   
    <uses-permission android:name="android.permission.CAMERA" />   
 
    <uses-feature android:name="android.hardware.camera" />   
    <uses-feature android:name="android.hardware.camera.autofocus" />   
 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />   
    <uses-permission android:name="android.permission.INTERNET" />   
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />   
 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />   
 <uses-permission android:name="android.permission.READ_PHONE_STATE" />   
 
    <application   
        android:allowBackup="true"   
        android:icon="@drawable/ic_launcher"   
        android:label="@string/app_name"   
        android:theme="@style/AppTheme" >   
        <activity   
            android:name="com.example.photopic.MainActivity"   
            android:label="@string/app_name"   
            android:screenOrientation="portrait"    
            >   
            <intent-filter>   
                <action android:name="android.intent.action.MAIN" />   
 
                <category android:name="android.intent.category.LAUNCHER" />   
            </intent-filter>   
        </activity>   
    </application>   
 
    </manifest>



Je n'arrive pas à trouver mon problème. Est ce que quelqu'un serait à même de m'aider ?

Merci d'avance à tous !