我无法检索刚拍摄的照片。我认为我的OnActivityResult有问题,例如CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE。从案例1中检索相册中的照片工作正常。请亲切的建议。
源代码:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if(resultCode == RESULT_OK){
Uri targetUri = (data != null) ? data.getData() : null;
if (targetUri == null) {
targetUri = fileUri;
}
imageView.setImageURI(targetUri);
}
if(resultCode == RESULT_CANCELED){
Uri targetUri = (data != null) ? data.getData() : null;
if (targetUri == null) {
targetUri = fileUri;
}
imageView.setImageURI(targetUri);
}
else{ ;
Uri targetUri = (data != null) ? data.getData() : null;
if (targetUri == null) {
targetUri = fileUri;
}
imageView.setImageURI(targetUri);
}
}
else if(requestCode == 1) {
if(resultCode == RESULT_OK){
Uri selectedImage = data.getData();
imageView.setImageURI(selectedImage);
}
}
}
}
答案 0 :(得分:2)
不幸的是,您是否获得图像取决于手机制造商。每个Android版本和每个制造商似乎都有一个略有不同的行为。有时URL在结果中作为“数据”参数提供(这就是你正在做的事情),但有时它只是空的。我甚至得到了一个返回RESULT_CANCEL的手机,虽然它工作得很好。
因为似乎没有办法实现所有类型的不同后备。这是我目前在我们的一个项目中使用的代码。成员变量mTargetUri
需要设置为您提供的输出文件名作为Intent参数MediaStore.EXTRA_OUTPUT
,mCaptureTime
应该在启动Intent之前设置为Sytem.currentTimeMillis()
。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Log.v(LOG_TAG, "Camera intent succeeded");
}
else if (resultCode == RESULT_CANCELED) {
Log.i(LOG_TAG, "Camera intent aborted");
}
else {
Log.e(LOG_TAG, "Camera intent failed with result code: " + resultCode);
}
// Regardless of the resultCode we're going to check if a new photo has been
// created on the phone. At least on the Samsung Galaxy S3 the behavior
// could be observed that although the result code was "0" the camera app
// created two (!) files on the SD card.
// Image captured and saved to fileUri specified in the Intent
Uri targetUri = (data != null) ? data.getData() : null;
if (targetUri == null) {
Log.w(LOG_TAG, "Camera intent returned empty result.");
targetUri = mTargetUri;
}
if (targetUri != null) {
String targetFilePath = targetUri.getPath();
File targetFile = new File(targetFilePath);
if (targetFile.exists()) {
Log.i(LOG_TAG, "Image saved to: " + targetUri.toString());
// Fix for issue reported here: http://code.google.com/p/android/issues/detail?id=22822
// and here: http://code.google.com/p/android/issues/detail?id=19268
// We're following the proposed solution from https://stackoverflow.com/questions/8450539/
int rotation = -1;
long fileSize = targetFile.length();
Cursor mediaCursor = getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[] {
MediaStore.Images.ImageColumns.ORIENTATION,
MediaStore.MediaColumns.SIZE,
MediaStore.MediaColumns.DATA },
MediaStore.MediaColumns.DATE_ADDED + ">=?",
new String[] { String.valueOf(mCaptureTime/1000 - 1)},
MediaStore.MediaColumns.DATE_ADDED + " desc");
if ((mediaCursor != null) && (mCaptureTime != 0)) {
if (mediaCursor.moveToFirst()) {
do {
long size = mediaCursor.getLong(1);
Uri uri = Uri.parse(mediaCursor.getString(2));
// Extra check to make sure that we are getting the orientation from the proper file
if (size == fileSize && !uri.equals(targetUri.toString())) {
rotation = mediaCursor.getInt(0);
break;
}
} while (mediaCursor.moveToNext());
}
mediaCursor.close();
}
if (rotation == -1) {
// It seems that there has been no duplication and no rotation issue so far. This means we can
// add our newly created file to the media library.
// TODO
}
else {
// Looks like the picture already exists in the media library. This indicates we got a duplicate.
Log.w(LOG_TAG, "Duplicate image found for " + targetUri.toString() + " in media library. Deleting the copy.");
if (!targetFile.delete()) {
Log.e(LOG_TAG, "Failed to delete duplicate image.");
}
}
}
}
}
}
代码源自Stackoverflow和其他站点上的不同来源。我们的基本想法是
RESULT_OK
或RESULT_CANCELED
),因为它们不表示照片是否已拍摄并存储在SD卡上。上面的代码只记录了值,但在任何情况下都会继续。mTargetUri
),希望我们能在那里找到新文件。targetUri
中指定的方式创建第二个副本名称。这种方法的问题是我们最终得到了重复的图像。为了解决这个问题,我们遵循从以下Stackoverflow问题派生的逻辑:Images taken with ACTION_IMAGE_CAPTURE always returns 1 for ExifInterface.TAG_ORIENTATION on some newer devices。我不会在这里详细介绍(请点击链接以获得更好的理解)但基本的想法是我们查询Android媒体商店并通过比较时间戳来检查它是否已包含我们的新照片(mCaptureTime
最初启动捕获意图时设置为System.currentTimeMillis()
。 这种逻辑应该适用于几乎所有Android设备的所有情况。到目前为止,我们已在十几种不同的设备和Android版本上对其进行了测试,但没有其他问题。
答案 1 :(得分:2)
我遇到同样的问题,一旦检查你是否有任何与历史相关的标签?
请勿将android:noHistory="true"
标记放入清单
如果您在清单中的活动中使用android:noHistory="true"
,它会
停止后从堆栈中移除。
注意:如果您使用的是标签活动,那么您也不应该使用android:noHistory="true"
或者只是将android:noHistory="false"
放在清单内的活动中。
答案 2 :(得分:0)
尝试转动相机并以风景模式拍摄照片。 Mediastore Camera Intent似乎只拍摄风景照片。