【发布时间】:2015-12-06 16:02:42
【问题描述】:
在我的 android 应用程序中,我使用了一个有时有效有时无效的相机活动。即使相机图片存储在手机上的图片目录中,大约 30% 的 uri 设置用于存储照片的时间都返回空值。下面是我的开始活动。
Uri imageUri; // These are defined at the global level
Uri selectedImage;
...............
String fileName = name.replace(" ", "");
fileName = fileName + suffix + ".jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
values.put(MediaStore.Images.Media.DESCRIPTION, "Image capture by camera");
imageUri = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, RESULT_LOAD_IMAGE);
}
else
Toast.makeText(getApplicationContext(), "Photo was not taken", Toast.LENGTH_SHORT).show();
下面是我的结果活动。
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Bitmap image1;
if(requestCode == RESULT_CROP_IMAGE)
{
Bundle extras = data.getExtras();
image1 = extras.getParcelable("data");
}
else {
if (requestCode == RESULT_LOAD_IMAGE) {
selectedImage = imageUri; // returns null occassionally
} else
selectedImage = data.getData();
if(selectedImage == null)
{
Toast.makeText(getApplicationContext(), "Photo not captured correctly", Toast.LENGTH_LONG).show();
return;
}
问题是变量 imageUri 偶尔会返回 null 每个人,即使图片是拍摄的并且驻留在三星 S4 手机图片目录中。谁能解释为什么会发生这种情况以及如何解决?如果返回空值,还有其他方法可以捕获照片吗?我应该补充一下,我使用的是 Android SDK 版本 23。我以前没有遇到过这个问题。
我有更多关于这个问题的信息。我在 startactivity 中设置我的图像路径如下:
String mCurrentPhotoPath; // is stored at the global level
.....
String fileName = name.replace(" ", "");
fileName = fileName + suffix + ".jpg";
mCurrentPhotoPath = fileName;
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, mCurrentPhotoPath);
values.put(MediaStore.Images.Media.DESCRIPTION, "Image capture by camera");
imageUri = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
在我的结果活动中,mCurrentPhotoPath 为空。是什么原因导致这件事被淘汰?我没有在任何其他会导致它变为 null 的地方引用 mCurrentPhotoPath。
if (requestCode == RESULT_LOAD_IMAGE) {
if(imageUri != null)
selectedImage = imageUri;
else
{
if(mCurrentPhotoPath != null) { // mCurrentPhotoPath returns null;
File file = new File(Environment.getExternalStorageDirectory().getPath(), mCurrentPhotoPath);
selectedImage = Uri.fromFile(file);
}
else {
Toast.makeText(getApplicationContext(), "Photo not captured", Toast.LENGTH_LONG).show();
return;
}
}
谁能解释为什么当我没有在除了开始活动和结果活动之外的任何其他地方引用它时,mCurrentPath 返回一个空值?
【问题讨论】:
标签: android android-camera-intent