Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android MediaStore.ACTION\u IMAGE\u捕获后onActivityResult中的位图为空_Android_Camera - Fatal编程技术网

Android MediaStore.ACTION\u IMAGE\u捕获后onActivityResult中的位图为空

Android MediaStore.ACTION\u IMAGE\u捕获后onActivityResult中的位图为空,android,camera,Android,Camera,我正在调用“活动”中的默认照相机应用程序以捕获图像。我遵循了安卓网站上的指南 private File createImageFile() throws IOException { // Create an image file name String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); String imageFileName = "JPEG_" + timeSt

我正在调用“活动”中的默认照相机应用程序以捕获图像。我遵循了安卓网站上的指南

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

    File image = File.createTempFile(
        imageFileName,  /* prefix */
        ".jpg",         /* suffix */
        storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = "" + image.getAbsolutePath();
    return image;
}

private void dispatchTakePictureIntent() {
    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
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Continue only if the File was successfully created
        if (photoFile != null) {
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(photoFile));
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }
}
现在相机启动了,我拍摄了图像。当我通过
onActivityResult
返回我的应用程序时,图像似乎没有保存

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
        try {
            //convert the image to base64
            Bitmap bm = BitmapFactory.decodeFile(mCurrentPhotoPath);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();  
            bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object   
            byte[] b = baos.toByteArray();
            String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);     
        } catch (Exception e){
            e.printStackTrace();
            Toast.makeText(context, "Whoops! Some error occured while taking that photo. Please try again.", Toast.LENGTH_LONG).show();
        }
    }
}
位图
为空


为什么为空???

您没有初始化文件路径。试试这个:

Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};

Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();

int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String mCurrentPhotoPath = cursor.getString(columnIndex);
cursor.close();

Bitmap bm = BitmapFactory.decodeFile(mCurrentPhotoPath);

检查您的mCurrentPhotoPath。它在onActivityResult()中似乎未初始化。mCurrentPhotoPath是在createImageFile方法中设置的。您是对的。刚刚查过,onActivityResult()中的CurrentPhotoPath为空。为什么会这样?试试下面给出的答案,我相信你的问题会得到解决。