【问题标题】:Android Camera Example - Not Saving ImageAndroid 相机示例 - 不保存图像
【发布时间】:2015-10-16 17:18:20
【问题描述】:

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

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

感谢您的帮助!

清单:

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

【问题讨论】:

  • “它似乎根本无法保存完整图像”——请准确解释您的症状是什么。你在崩溃吗?您是否在ImageView 中获取图像?如果您没有看到该文件,您是如何尝试查找该文件的(桌面操作系统文件管理器?DDMS?adb?还有别的吗?)。
  • 使用 setPic() 会崩溃,因为它试图除以零(photoW/targetW),即我的 Logger 打印出大小之后的行。我试过查看我的实际设备,但找不到任何文件。我不太确定我还能做些什么来尝试找到 Android 将图像放在哪里,或者它是如何尝试找到它的。同样使用 galleryAddPic() 函数实际上不会向我的画廊添加任何内容。

标签: android bitmap android-camera


【解决方案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() (BitmapFactory.decodeFile(mCurrentPhoto.getAbsolutePath(), bmOptions))。

【讨论】:

  • 我做了这些更改,它解决了我的大部分问题,谢谢!图像现在可以正确保存,我可以在我的存储中看到它,并使用 BitmapFactory.decodeFile 将其取回。虽然,galleryAddPic() 函数似乎没有将它添加到我的画廊,所以我在我的照片应用程序中看不到它。由于某种原因,imageView.getWidth() 仍然返回 0,所以如果我不尝试按比例缩放,它确实会正确显示。
  • @Awestruck:“虽然,galleryAddPic() 函数似乎没有将它添加到我的画廊,所以我在我的照片应用程序中看不到它”——我使用 MediaScannerConnection 和它的静态scanFile() 方法,个人。 “imageView.getWidth() 仍然返回 0”——你确定在这段代码运行时 ImageView 已经布局了吗?而且,你是给ImageView 一个明确的尺寸,还是使用wrap_content 作为尺寸?
猜你喜欢
  • 1970-01-01
  • 2012-12-20
  • 2017-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-20
  • 2011-06-16
相关资源
最近更新 更多