Android相机示例 - 不保存图像

时间:2015-10-16 17:18:21

标签: android bitmap android-camera

我目前正在尝试在我的应用中实施相机以拍摄完整图像并保存。我按照“保存全尺寸照片”下的android.com指南进行操作。部分。

该教程的第一部分没有问题,但似乎根本没有保存完整的图像。当使用setPic函数时,它会崩溃,因为它获得的Bitmap的大小为0. addGalleryPic函数似乎也没有向库中添加任何东西。

感谢您的帮助!

Manifiest:

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

的活动:

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

覆盖活动结果。

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

1 个答案:

答案 0 :(得分:1)

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

  • mCurrentPhotoPath = "file:" + image.getAbsolutePath();导致的值既不是有效的文件系统路径,也不是Uri的有效字符串表示

  • File f = new File(mCurrentPhotoPath);会导致File无效,因为您在上面的项目符号中放置了文件系统路径

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

大多数情况下,您仍在使用file:。然后对于File,只需在此时调用decodeFile()getAbsolutePath())。