Android相机拍照后预览黑色图片

时间:2018-01-08 00:54:44

标签: android android-emulator android-camera-intent

我有一个小应用程序打开相机预览,你点击按钮拍照,然后它会显示在屏幕上。我使用模拟器通过Nexus 5 API 25进行预览。

当我启动应用程序时,我可以看到相机预览应该是。但是,当我点击拍摄照片时,它会显示图像应该是黑色的屏幕,而imageView.setBitmap(photo)也会显示此黑色图像。

我一直在关注本教程:https://www.youtube.com/watch?v=k1Wc0vmD284并且其他人似乎没有这个问题。

可能是什么问题?是否与图像的尺寸调整后有关?我已经为我的清单文件添加了文件读写权限,并且照片预览功能无需拍照,因此我假设我有权使用相机。

以下是我的代码:

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

imageView = (ImageView) findViewById(R.id.imageView);

Button myButton = (Button) findViewById(R.id.myButton);
    imageView = (ImageView) findViewById(R.id.imageView);

    //disable button if user has no camera
    if(!hasCamera() ) {
        myButton.setEnabled(false);
    }
}



//Check if the user has a camrea
private boolean hasCamera() {
    return getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY);
}


//Launching the camera
public void launchCamera(View view ){
  Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {

       Bundle extras = data.getExtras();
       Bitmap photo = (Bitmap) extras.get("data");
       imageView.setImageBitmap(photo)
}

1 个答案:

答案 0 :(得分:0)

private Uri fileUri;
public static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;
 private void launchCamera() {
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
            Locale.getDefault()).format(new Date());
    File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImage");
    imagesFolder.mkdirs();
    File image = new File(imagesFolder, "IMG_" + timeStamp + ".jpg");
    Uri uriSavedImage = Uri.fromFile(image);

    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    startActivityForResult(cameraIntent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);

    fileUri=uriSavedImage;

}

private boolean hasCamera() {
    if (getApplicationContext().getPackageManager().hasSystemFeature(
            PackageManager.FEATURE_CAMERA)) {
        // this device has a camera
        return true;
    } else {
        // no camera on this device
        return false;
    }
}

 @Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

        // save file url in bundle as it will be null on scren orientation
    // changes
    outState.putParcelable("file_uri", fileUri);
}

    @Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);

    // get the file url
    fileUri = savedInstanceState.getParcelable("file_uri");
}
 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // if the result is capturing Image
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case CAMERA_CAPTURE_IMAGE_REQUEST_CODE:
            if (resultCode == RESULT_OK) {
                // successfully captured the image
                // display it in image viewproduct_image
                Bitmap photo = (Bitmap) data.getExtras().get("data");

                storeImage(photo);

            } else if (resultCode == RESULT_CANCELED) {
                // user cancelled Image capture
                Toast.makeText(getApplicationContext(),
                        "User cancelled image capture", Toast.LENGTH_SHORT)
                        .show();
            } else {
                // failed to capture image
                Toast.makeText(getApplicationContext(),
                        "Sorry! Failed to capture image", Toast.LENGTH_SHORT)
                        .show();
            }
            break;
}
}
    private void storeImage(Bitmap image) {

    if (fileUri == null) {
        Log.d("TAG",
                "Error creating media file, check storage permissions: ");// e.getMessage());
        return;
    }
    try {
        FileOutputStream fos = new FileOutputStream(fileUri.getPath());
        image.compress(Bitmap.CompressFormat.JPEG, 90, fos);
        fos.close();
    } catch (FileNotFoundException e) {
        Log.d("TAG", "File not found: " + e.getMessage());
    } catch (IOException e) {
        Log.d("TAG", "Error accessing file: " + e.getMessage());
    }

    Bitmap bitmap1= BitmapFactory.decodeFile(fileUri.getPath());
    imageView.setImageBitmap(bitmap1);
}
  public static boolean checkIfAlreadyhavePermission(Activity activity, String permission) {
    int result = ContextCompat.checkSelfPermission(activity, permission);
    if (result == PackageManager.PERMISSION_GRANTED) {
        return true;
    } else {
        return false;
    }
}
public static void requestForSpecificPermission(Activity activity, String granted_permission) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        activity.requestPermissions(new String[]{granted_permission}, 101);
    }
}
  protected void onResume() {
    super.onResume();
if( checkIfAlreadyhavePermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE) ){

    }else {
        requestForSpecificPermission(
                activity, Manifest.permission.READ_EXTERNAL_STORAGE);
    }

    if( checkIfAlreadyhavePermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) ){


    }else {
        requestForSpecificPermission(
                activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    }
    if (checkIfAlreadyhavePermission(activity, Manifest.permission.CAMERA)){

    }else {
        requestForSpecificPermission(
                activity, Manifest.permission.CAMERA);
    }
}

尝试上面的代码,直到android 8