Android 相机示例 - 不保存图像

Android Camera Example - Not Saving Image

我目前正在尝试在我的应用程序中实现相机以拍摄完整图像并保存。我正在按照 'Save the Full-Size Photo' 部分下 android.com 的指南进行操作。

该教程的第一部分没有问题,但由于某种原因似乎无法保存完整图像。使用 setPic 函数时,它会崩溃,因为它获取的位图大小为 0。addGalleryPic 函数似乎也没有向画廊添加任何内容。

感谢您的帮助!

清单:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Activity:

String mCurrentPhotoPath;
static final int REQUEST_TAKE_PHOTO = 1;

正在创建文件。

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 = "file:" + 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 (IOException ex) {
            // Error occurred while creating the File

        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                    Uri.fromFile(photoFile));
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }
}

覆盖 Activity 结果。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Logger.d( "onResult: " + requestCode + " & " + resultCode  );
    if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {

        Logger.d( "Attempting to open: " + mCurrentPhotoPath );
        galleryAddPic();
        setPic();
    }
}

正在将图像添加到画廊。

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);
}

并将图像设置到 imageView。

private void setPic() {
    // Get the dimensions of the View
    int targetW = image.getWidth();
    int targetH = image.getHeight();

    // Get the dimensions of the bitmap
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;


    // Both of these values are zero.
    Logger.d( "Size: " + photoW + "x" + photoH );

    // Determine how much to scale down the image
    // **THIS LINE CRASHES - Divide by zero ( size is zero ).**
    int scaleFactor = Math.min(photoW/targetW, photoH/targetH);

    // Decode the image file into a Bitmap sized to fill the View
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inPurgeable = true;

    Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
    image.setImageBitmap(bitmap);
}

我建议您删除 String mCurrentPhotoPath 并将其替换为 File mCurrentPhoto(或您认为合适的其他名称)。这将清除一些错误:

  • mCurrentPhotoPath = "file:" + image.getAbsolutePath(); 产生的值既不是有效的文件系统路径,也不是 Uri

  • 的有效字符串表示形式
  • File f = new File(mCurrentPhotoPath); 导致无效 File,因为你在上面的项目符号

    中放置的文件系统路径是流氓 file:
  • BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions); 将不起作用,因为流氓 file: 您在第一个项目符号中放置了文件系统路径

大多数时候,您都在使用 File。然后对于 decodeFile(),此时只需调用 getAbsolutePath() (BitmapFactory.decodeFile(mCurrentPhoto.getAbsolutePath(), bmOptions))。