【发布时间】:2016-04-04 15:25:16
【问题描述】:
我想了解这两种情况下发生了什么。我直接使用来自http://developer.android.com/training/camera/photobasics.html官方文档的代码
假设 Manifest 文件中存在所有必要的读/写权限。
场景 1:
static final int REQUEST_IMAGE_CAPTURE = 1;
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
mImageView.setImageBitmap(imageBitmap);
}
}
这段代码似乎对我有用,但我不知道它到底在做什么。它基本上启动相机并让我拍照,然后在 onActivityResult 中,我可以通过这种方式获取缩略图,但将其从意图中拉出。
场景 2:
String mCurrentPhotoPath;
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;
}
static final int REQUEST_TAKE_PHOTO = 1;
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) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras(); //data is null here!!!
Bitmap imageBitmap = (Bitmap) extras.get("data");
mImageView.setImageBitmap(imageBitmap);
}
}
另一方面,这种情况应该让我获得完整的文件,但它不起作用并为我抛出错误,因为 Intent 变量为空,所以我无权访问任何内容。
我的问题:
当您使用 Android 相机拍照时,它通常会创建两个文件——一个缩略图和一个完整文件吗?
在方案 1 中,这些文件不会写入内部或外部存储,而只是写入 RAM,对吗?缩略图保存在 Intent 中,但这是否意味着整个文件丢失/无法访问?
在场景 2 中,Intent 为 null,因为我出于某种原因使用了
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));。我怎么知道这个文件在哪里?我真的需要mPhotoPath还是应该继续访问File photoFile,在我到达onActivityResult 方法时我已经丢失了?保留此文件的正确做法是什么?使其成为成员变量?这个例子中的图像创建函数使用
getExternalStoragePublicDirectory——这和保存在内部存储中是不同的吗?我曾假设有很多方法可以保存文件:内部存储、RAM、SD 卡、连接到手机的存储设备(例如外部 HDD)、云等。我怎么知道什么是什么?
【问题讨论】:
标签: android android-intent bitmap android-camera