【发布时间】: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