拍照时,数据返回为空

时间:2016-04-11 14:59:39

标签: android android-camera

我试图将拍摄的照片缩略图设置为ImageButton的scr。以下代码可以正常工作,拍摄照片并将其存储在图库中,但是" Bitmap imageBitmap =(Bitmap)extras.get(" data");"返回null。 有人可以解释一下原因吗?

private void openCamera() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        // Create the File where the photo should go
        photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File
            Log.i("Camera log", "Failed:" + ex.toString());
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                    Uri.fromFile(photoFile));

            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }
}

private void galleryAddPic() {
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    File f = new File(mCurrentPhotoPath);
    Uri contentUri = Uri.fromFile(f);
    mediaScanIntent.setData(contentUri);
    this.sendBroadcast(mediaScanIntent);
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");
        cameraBtn.setImageBitmap(imageBitmap);
        galleryAddPic();
    }
}

2 个答案:

答案 0 :(得分:1)

当您设置if(photoFile!=null){ BitmapFactory.Options bmOptions = null; bmOptions = new BitmapFactory.Options(); Bitmap mBitmap = BitmapFactory.decodeFile(photoFile.getPath(), bmOptions); } else{ Bundle extras = data.getExtras(); Bitmap imageBitmap = (Bitmap) extras.get("data"); cameraBtn.setImageBitmap(imageBitmap); galleryAddPic(); } 时,您必须从此网址获取照片。

示例:

OrderBy

答案 1 :(得分:0)

  

任何人都可以解释原因吗?

您提供了// [...] net.createServer(function(sock) { sock.on('data', function(data) { sock.write(data); }); } // [...] 。您的图像应存储在您指定的位置。如果你提供EXTRA_OUTPUT,那么那么 EXTRA_OUTPUT应该有你的extras.get("data")(至少如果相机应用程序你的应用程序结束了使用没有错误。)