カメラアプリケーションを起動すると、このエラーが発生します。これが私のlogcatです。
12-08 12:50:10.393 2275-2369/com.Android.launcher3 E/Surface: getSlotFromBufferLocked: unknown buffer: 0xa21675b0
12-08 12:50:10.509 965-3048/? E/CameraService: CameraService::connect X (PID 3209) rejected (invalid camera ID 0)
12-08 12:50:10.510 3209-3224/com.Android.camera E/CameraHolder: fail to connect Camera
12-08 12:50:10.510 3209-3224/com.Android.camera E/CameraHolder: Java.lang.RuntimeException: Fail to connect to camera service
12-08 12:50:10.510 3209-3224/com.Android.camera E/CameraHolder: at Android.hardware.Camera.<init>(Camera.Java:495)
12-08 12:50:10.510 3209-3224/com.Android.camera E/CameraHolder: at Android.hardware.Camera.open(Camera.Java:341)
12-08 12:50:10.510 3209-3224/com.Android.camera E/CameraHolder: at com.Android.camera.CameraHolder.open(CameraHolder.Java:131)
12-08 12:50:10.510 3209-3224/com.Android.camera E/CameraHolder: at com.Android.camera.Util.openCamera(Util.Java:267)
12-08 12:50:10.510 3209-3224/com.Android.camera E/CameraHolder: at com.Android.camera.Camera$3.run(Camera.Java:1100)
12-08 12:50:10.510 3209-3224/com.Android.camera E/CameraHolder: at Java.lang.Thread.run(Thread.Java:818)
12-08 12:50:10.511 3209-3224/com.Android.camera E/AndroidRuntime: FATAL EXCEPTION: Thread-179
12-08 12:50:10.511 3209-3224/com.Android.camera E/AndroidRuntime: Process: com.Android.camera, PID: 3209
12-08 12:50:10.511 3209-3224/com.Android.camera E/AndroidRuntime: Java.lang.RuntimeException: openCamera failed
12-08 12:50:10.511 3209-3224/com.Android.camera E/AndroidRuntime: at com.Android.camera.Util.openCamera(Util.Java:272)
12-08 12:50:10.511 3209-3224/com.Android.camera E/AndroidRuntime: at com.Android.camera.Camera$3.run(Camera.Java:1100)
12-08 12:50:10.511 3209-3224/com.Android.camera E/AndroidRuntime: at Java.lang.Thread.run(Thread.Java:818)
12-08 12:50:10.511 3209-3224/com.Android.camera E/AndroidRuntime: Caused by: com.Android.camera.CameraHardwareException: Java.lang.RuntimeException: Fail to connect to camera service
12-08 12:50:10.511 3209-3224/com.Android.camera E/AndroidRuntime: at com.Android.camera.CameraHolder.open(CameraHolder.Java:135)
12-08 12:50:10.511 3209-3224/com.Android.camera E/AndroidRuntime: at com.Android.camera.Util.openCamera(Util.Java:267)
12-08 12:50:10.511 3209-3224/com.Android.camera E/AndroidRuntime: at com.Android.camera.Camera$3.run(Camera.Java:1100)
12-08 12:50:10.511 3209-3224/com.Android.camera E/AndroidRuntime: at Java.lang.Thread.run(Thread.Java:818)
12-08 12:50:10.511 3209-3224/com.Android.camera E/AndroidRuntime: Caused by: Java.lang.RuntimeException: Fail to connect to camera service
12-08 12:50:10.511 3209-3224/com.Android.camera E/AndroidRuntime: at Android.hardware.Camera.<init>(Camera.Java:495)
12-08 12:50:10.511 3209-3224/com.Android.camera E/AndroidRuntime: at Android.hardware.Camera.open(Camera.Java:341)
12-08 12:50:10.511 3209-3224/com.Android.camera E/AndroidRuntime: at com.Android.camera.CameraHolder.open(CameraHolder.Java:131)
12-08 12:50:10.511 3209-3224/com.Android.camera E/AndroidRuntime: at com.Android.camera.Util.openCamera(Util.Java:267)
12-08 12:50:10.511 3209-3224/com.Android.camera E/AndroidRuntime: at com.Android.camera.Camera$3.run(Camera.Java:1100)
12-08 12:50:10.511 3209-3224/com.Android.camera E/AndroidRuntime: at Java.lang.Thread.run(Thread.Java:818)
12-08 12:50:10.671 954-954/? E/EGL_emulation: tid 954: eglCreateSyncKHR(1243): error 0x3004 (EGL_BAD_ATTRIBUTE)
これは私のconfig.iniです
avd.ini.encoding=UTF-8
AvdId=Nexus_S_API_23
abi.type=x86
avd.ini.displayname=Nexus S API 23
disk.dataPartition.size=200M
hw.accelerometer=yes
hw.audioInput=yes
hw.battery=yes
hw.camera.back=webcam0
hw.camera.front=none
hw.cpu.Arch=x86
hw.dPad=no
hw.device.hash2=MD5:37a2ff6e511626ba3ceddec8264474be
hw.device.manufacturer=Google
hw.device.name=Nexus S
hw.gps=yes
hw.gpu.enabled=yes
hw.keyboard=yes
hw.lcd.density=240
hw.mainKeys=yes
hw.ramSize=343
hw.sdCard=yes
hw.sensors.orientation=yes
hw.sensors.proximity=yes
hw.trackBall=no
image.sysdir.1=system-images\Android-23\google_apis\x86\
runtime.network.latency=none
runtime.network.speed=full
runtime.scalefactor=auto
sdcard.size=100M
skin.dynamic=yes
skin.name=nexus_s
skin.path=C:\Program Files\Android\Android Studio\plugins\Android\lib\device-art-resources\nexus_s
snapshot.present=no
tag.display=Google APIs
tag.id=google_apis
vm.heapSize=32
私もこのエラーに直面しました。
emulator -webcam-list -avd Nexus_S_API_23
emulator: WARNING: Increasing RAM size to 1GB
emulator: ERROR: camera_device_start_capturing: Device 'AndroidEmulatorVC0' is unable to save frame to the clipboard: 0
注:Androidブラウザーから画像をダウンロードしようとするとエラーが発生します。
編集
これらのいずれかを試しましたか?カメラサービスが適切に機能しなかったようです/カメラは別のアプリケーションに使用されている可能性があります。
ちなみにMacOSXを使用します。私はそれを言って、それは私のマシンで動作します。インストールしたツールとログを送信しました。たぶんそれが役立ちます。
VD設定
Nexus 5, API 23 (Both Cameras are selected)
エミュレータを起動すると、ログに警告が表示されますが、最後には機能しました。
ログ
警告:接続されたカメラのリストにカメラ名「webcam0」が見つかりません。
12-12 22:59:15.060 2060-2079/com.Android.camera V/CameraHolder: open camera 0
12-12 22:59:15.060 1019-1730/? I/CameraService: CameraService::connect call (PID 2060 "com.Android.camera", camera ID 0) for HAL version default and Camera API version 1
12-12 22:59:15.060 1019-1730/? V/EmulatedCamera_Camera: getCameraInfo
12-12 22:59:15.060 1019-1730/? V/EmulatedCamera_BaseCamera: getCameraInfo
12-12 22:59:15.060 1019-1730/? W/AudioFlinger: acquireAudioSessionId() unknown client 2060 for session 7
12-12 22:59:15.060 1019-2081/? D/NuPlayerDriver: notifyListener_l(0xf619af80), (1, 0, 0)
12-12 22:59:15.060 1019-1730/? W/AudioFlinger: acquireAudioSessionId() unknown client 2060 for session 8
12-12 22:59:15.070 1019-2083/? D/NuPlayerDriver: notifyListener_l(0xf619afe0), (1, 0, 0)
12-12 22:59:15.070 1019-1730/? I/CameraClient: Opening camera 0
12-12 22:59:15.070 1019-1730/? V/EmulatedCamera_Camera: getCameraInfo
12-12 22:59:15.070 1019-1730/? V/EmulatedCamera_BaseCamera: getCameraInfo
12-12 22:59:15.070 1019-1730/? V/EmulatedCamera_Camera: connectCamera
12-12 22:59:15.070 1019-1730/? V/EmulatedCamera_QemuDevice: connectDevice
12-12 22:59:15.100 2059-2059/? D/AndroidRuntime: Calling main entry com.Android.commands.pm.Pm
12-12 22:59:15.110 1019-1730/? V/EmulatedCamera_QemuDevice: connectDevice: Connected to device 'webcam0'
12-12 22:59:15.110 1019-1730/? V/EmulatedCamera_CallbackNotifier: setCallbacks: 0xf75d1505, 0xf75d1dba, 0xf75d1c92, 0xf75d2267 (0xf619b040)
12-12 22:59:15.110 1019-1730/? V/EmulatedCamera_CallbackNotifier: enableMessage: msg_type = 0xc0d
12-12 22:59:15.110 1019-1730/? V/EmulatedCamera_CallbackNotifier: CAMERA_MSG_ERROR
12-12 22:59:15.110 1019-1730/? V/EmulatedCamera_CallbackNotifier: CAMERA_MSG_FOCUS
12-12 22:59:15.110 1019-1730/? V/EmulatedCamera_CallbackNotifier: CAMERA_MSG_ZOOM
12-12 22:59:15.110 1019-1730/? V/EmulatedCamera_CallbackNotifier: CAMERA_MSG_PREVIEW_METADATA
12-12 22:59:15.110 1019-1730/? V/EmulatedCamera_CallbackNotifier: **** Currently enabled messages:
12-12 22:59:15.110 1019-1730/? V/EmulatedCamera_CallbackNotifier: CAMERA_MSG_ERROR
12-12 22:59:15.110 1019-1730/? V/EmulatedCamera_CallbackNotifier: CAMERA_MSG_FOCUS
12-12 22:59:15.110 1019-1730/? V/EmulatedCamera_CallbackNotifier: CAMERA_MSG_ZOOM
12-12 22:59:15.110 1019-1730/? V/EmulatedCamera_CallbackNotifier: CAMERA_MSG_PREVIEW_METADATA
12-12 22:59:15.120 1350-1374/system_process I/ActivityManager: Start proc 2086:com.Android.defcontainer/u0a3 for service com.Android.defcontainer/.DefaultContainerService
12-12 22:59:15.160 2086-2098/com.Android.defcontainer D/DefContainer: Copying /data/local/tmp/com.example.volkangurol.myapplication to base.apk
12-12 22:59:15.210 1449-1449/com.Android.systemui D/PhoneStatusBar: disable: < expand ICONS alerts SYSTEM_INFO back home* recent* clock search* quick_settings >
12-12 22:59:15.210 2060-2103/com.Android.camera D/Camera: app passed NULL surface
12-12 22:59:15.210 1019-1619/? V/EmulatedCamera_Camera: getCameraInfo
12-12 22:59:15.210 1019-1619/? V/EmulatedCamera_BaseCamera: getCameraInfo
12-12 22:59:15.230 2060-2103/com.Android.camera V/camera: Preview size is 640x480
12-12 22:59:15.230 1019-1019/? V/EmulatedCamera_Camera: setParameters
12-12 22:59:15.230 1019-1019/? D/EmulatedCamera_Camera: === Value changed: jpeg-quality: 90 -> 95
12-12 22:59:15.230 1019-1019/? D/EmulatedCamera_Camera: === Value changed: preview-frame-rate: 24 -> 30
12-12 22:59:15.230 1019-1019/? D/EmulatedCamera_Camera: +++ New parameter: recording-hint=false
12-12 22:59:15.230 2060-2103/com.Android.camera V/camera: startPreview
12-12 22:59:15.230 1019-1380/? V/EmulatedCamera_Preview: setPreviewWindow: current: 0x0 -> new: 0x0
12-12 22:59:15.230 1019-1380/? V/EmulatedCamera_Camera: doStartPreview
12-12 22:59:15.230 1019-1380/? V/EmulatedCamera_Preview: startPreview
12-12 22:59:15.230 1019-1380/? D/EmulatedCamera_Camera: Starting camera: 640x480 -> NV21(yuv420sp)
12-12 22:59:15.230 1019-1380/? V/EmulatedCamera_QemuDevice: startDevice
12-12 22:59:15.230 1019-1380/? V/EmulatedCamera_Device: commonStartDevice: Allocated 0xf3f00000 460800 bytes for 307200 pixels in NV21[640x480] frame
12-12 22:59:15.230 1019-1380/? V/EmulatedCamera_QemuDevice: startDevice: Qemu camera device 'webcam0' is started for NV21[640x480] frames
12-12 22:59:15.230 1019-1380/? V/EmulatedCamera_Device: startDeliveringFrames
12-12 22:59:15.230 1019-1380/? V/EmulatedCamera_Device: startWorkerThread
12-12 22:59:15.260 1019-2104/? V/EmulatedCamera_Device: Starting emulated camera device worker thread...
Android API> = 23では、次のようなコードで許可を求める必要があります。
int REQUEST_PERMISSION= 100;
int cameraPermission = this.checkSelfPermission(Manifest.permission.CAMERA);
if (Android.os.Build.VERSION.SDK_INT >= 23) {
if (cameraPermission != PackageManager.PERMISSION_GRANTED ) {
this.requestPermissions(
new String[]{Manifest.permission.CAMERA },
REQUEST_PERMISSION
);
}
}
実行しても大丈夫です。
解決策は簡単です。VMカメラを開くアクションとWebカメラを開くアクションをリンクしない限り、仮想マシンの使用時にカメラを実行できません。
API 23以降、実行時に権限をリクエストする必要があります。これは、API <23には当てはまりませんでした。 APIの場合、マニフェスト内の<23の権限で十分です。
これはAndroid Developersの例です:
許可を確認します。
// Assume thisActivity is the current activity
int permissionCheck = ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.WRITE_CALENDAR);
許可をリクエストします。
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.READ_CONTACTS)) {
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
リクエストレスポンス:
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
詳細については、Android Developers:
http://developer.Android.com/training/permissions/requesting.html
そのように処理しないと、23以上のAPIでカメラを使用できません
ユーザーがすべての権限を付与する必要はありませんが、危険な権限(Camera、Location、)のみを付与する必要があります。
http://developer.Android.com/guide/topics/security/permissions.html#normal-dangerous
Developers.Android.comの引用:
Android 6.0(APIレベル23)以降、ユーザーはアプリのインストール時ではなく、アプリの実行中にアプリに権限を付与します。この方法では、アプリのインストールプロセスが合理化されます。アプリをインストールまたは更新するときに権限を付与する必要があります。また、ユーザーはアプリの機能をより細かく制御できます;たとえば、ユーザーはカメラアプリにカメラへのアクセスを許可しますが、デバイスの場所へのアクセスは許可しません。アプリの設定画面にアクセスして、いつでも権限を取り消すことができます。
システムのアクセス許可は、通常と危険の2つのカテゴリに分類されます。
- 通常の許可は、ユーザーのプライバシーを直接危険にさらしません。アプリのマニフェストに通常の許可がリストされている場合、システムは
自動的に許可。- 危険な権限は、ユーザーの機密データへのアクセスをアプリに与える可能性があります。アプリに通常の許可がリストされている場合
manifest、システムは許可を自動的に付与します。危険な許可をリストする場合、ユーザーは明示的に承認を与える必要があります
アプリ。
だから、私は次のように私のタスクを達成しました:
権限を確認するために、次のように別のクラスを作成しました:
public class MarshMallowPermission {
public static final int RECORD_PERMISSION_REQUEST_CODE = 1;
public static final int EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE = 2;
public static final int CAMERA_PERMISSION_REQUEST_CODE = 3;
Activity activity;
public MarshMallowPermission(Activity activity) {
this.activity = activity;
}
public boolean checkPermissionForRecord(){
int result = ContextCompat.checkSelfPermission(activity, Manifest.permission.RECORD_AUDIO);
if (result == PackageManager.PERMISSION_GRANTED){
return true;
} else {
return false;
}
}
public boolean checkPermissionForExternalStorage(){
int result = ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (result == PackageManager.PERMISSION_GRANTED){
return true;
} else {
return false;
}
}
public boolean checkPermissionForCamera(){
int result = ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA);
if (result == PackageManager.PERMISSION_GRANTED){
return true;
} else {
return false;
}
}
public void requestPermissionForRecord(){
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.RECORD_AUDIO)){
Toast.makeText(activity, "Microphone permission needed for recording. Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show();
} else {
ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.RECORD_AUDIO},RECORD_PERMISSION_REQUEST_CODE);
}
}
public void requestPermissionForExternalStorage(){
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)){
Toast.makeText(activity, "External Storage permission needed. Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show();
} else {
ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE);
}
}
public void requestPermissionForCamera(){
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.CAMERA)){
Toast.makeText(activity, "Camera permission needed. Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show();
} else {
ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.CAMERA},CAMERA_PERMISSION_REQUEST_CODE);
}
}
}
次に、取得するために
...
MarshMallowPermission marshMallowPermission = new MarshMallowPermission(this);
...
public void getPhotoFromCamera() {
if (!marshMallowPermission.checkPermissionForCamera()) {
marshMallowPermission.requestPermissionForCamera();
} else {
if (!marshMallowPermission.checkPermissionForExternalStorage()) {
marshMallowPermission.requestPermissionForExternalStorage();
} else {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File mediaStorageDir = new File(
Environment.getExternalStorageDirectory()
+ File.separator
+ getString(R.string.directory_name_corp_chat)
+ File.separator
+ getString(R.string.directory_name_images)
);
if (!mediaStorageDir.exists()) {
mediaStorageDir.mkdirs();
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
try {
mediaFile = File.createTempFile(
"IMG_" + timeStamp, /* prefix */
".jpg", /* suffix */
mediaStorageDir /* directory */
);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mediaFile));
startActivityForResult(takePictureIntent, PICK_FROM_CAMERA);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
PcまたはMacに動作するカメラがあるか、カメラシミュレーションがインストールされている必要があります。