【问题标题】:Android studio webcam0 camera emulator cannot takingAndroid studio webcam0 相机模拟器无法拍摄
【发布时间】:2015-03-07 02:07:15
【问题描述】:

我可以连接笔记本上的 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
        }
    }
}

【问题讨论】:

    标签: android camera


    【解决方案1】:

    我也遇到了同样的问题。显然,这个问题已经持续了 3 年多,现在即使在最新的 Android Studio 上,同样的问题仍然存在!

    最好的解决办法是切换到真机来测试/运行它!

    【讨论】:

      猜你喜欢
      • 2016-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-10
      • 1970-01-01
      • 1970-01-01
      • 2011-07-15
      • 1970-01-01
      相关资源
      最近更新 更多