Android相机Bug

时间:2016-06-27 20:08:07

标签: android android-camera

我想制作一个非常简单的应用程序,我想在相对布局中打开相机,但我只看到黑屏而不是相机。 代码我很简单

public class MainActivity extends AppCompatActivity {


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

public void button(android.view.View v){
    android.hardware.Camera camera = android.hardware.Camera.open();
    RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.video);
    SurfaceView surfaceView = new SurfaceView(getApplicationContext());

    relativeLayout.addView(surfaceView);

    SurfaceHolder surfaceHolder = surfaceView.getHolder();

    try {
        camera.setPreviewDisplay(surfaceHolder);
        camera.startPreview();
    } catch (IOException e) {};

}

}

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

使用下面的代码,您可以在单击按钮时打开相机,拍照并在图像视图中显示。 在测试/使用它之前,不要忘记编辑robocopy.exe $source $destination $files $backup $reportextrafiles $verboseoutput $fullpathname $mirror $backupmode $subfolders $Retries $consolewrite | ForEach-Object -Process { $data = $_.Split([char]9); if (($data.Count -gt 4) -and ("$($data[4])" -ne "")) { $file = "$($data[4])" Write-Progress "Percentage $($data[0])" -Activity "Documents" -CurrentOperation "$($file)" -ErrorAction SilentlyContinue; } else { Write-Progress "Percentage $($data[0])" -Activity "Documents" -CurrentOperation "$($file)" } } # Robocopy has a bitmask set of exit codes, so only complain about failures: [int] $exitCode = $global:LastExitCode; [int] $someCopyErrors = $exitCode -band 8; [int] $seriousError = $exitCode -band 16; if (($someCopyErrors -ne 0) -or ($seriousError -ne 0)) { Write-Error "ERROR: robocopy failed with a non-successful exit code: $exitCode" }

您的Java文件:

YOUR_PROJECT

您的XML:

package YOUR_PROJECT;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;


public class MainActivity extends AppCompatActivity {

ImageView iv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Initialize Image view and Buttons
    iv = (ImageView) findViewById(R.id.imageView);
    Button btnCapture = (Button) findViewById(R.id.button_camera);

    //Set listener on Capture button
    btnCapture.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent c = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //Implicit Intent
            startActivityForResult(c, 0);
        }
    });


}

//Override method onActivityResult used to retreive the image
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Bitmap m = (Bitmap) data.getExtras().get("data");
    iv.setImageBitmap(m);
    iv.setScaleType(ImageView.ScaleType.FIT_XY);
}
}