我可以在笔记本上连接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
}
}
}
答案 0 :(得分:0)
我遇到了同样的问题。显然,这个问题已经持续了3年多,现在即使在最新的Android Studio上也存在同样的问题!
最好的解决方法是切换到真正的手机来测试/运行它!