2
내 노트북에서 webcam0 카메라 에뮬레이터로 연결할 수 있지만 사진을 한 번만 가져갈 수 있습니다. 카메라를 클릭하여 두 번째 사진을 촬영하면 에뮬레이터에서 비디오 소스를 경고하여 비디오 장치를 선택하게합니다. 선택하고 확인을 클릭하면 경고 메시지가 표시됩니다. "불행히도 카메라가 중지되었습니다."Android 스튜디오 webcam0 카메라 에뮬레이터를 사용할 수 없습니다.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.friend.friend2" >
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" android:required="true"/>
public class Camera extends Activity {
Uri fileUri;
public static final int MEDIA_TYPE_IMAGE = 1;
public static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
public static String photoPath = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera);
Button btn2 = (Button) findViewById(R.id.cameraB);
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(Camera.MEDIA_TYPE_IMAGE); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
});
}
private static Uri getOutputMediaFileUri(int type){
return Uri.fromFile(getOutputMediaFile(type));
}
private static File getOutputMediaFile(int type){
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
// Folder Name
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "FriendProfile");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("FriendProfile", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE){
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");// <<picture file name
photoPath = ""+mediaFile.toString();
}
else {
return null;
}
return mediaFile;
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
//Toast.makeText(this, "Image saved to:\n" +data.getData(), Toast.LENGTH_LONG).show();
ImageView imgView = (ImageView) findViewById(R.id .imageView2);
imgView.setImageURI(fileUri);
Log.d("fileUri", fileUri.toString());
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
// Image capture failed, advise user
}
}
}