我正在开发一个新的应用程序。该应用程序使用相机和外部存储。虽然在我的 LG G3 上,该应用程序运行良好(以及在 AVD 模拟器上),但在三星 S5 上测试它时,当我从相机获得结果时,在拍摄照片后(在 onActivityResult 方法中)看起来像没有结果。意图为空。我只是添加并说照片正在拍摄,并保存在指定的位置,但由于某种原因我无法使用它。如前所述,相同的代码在我的 G3 上运行良好。
我还尝试从头开始创建一个相机测试应用程序,它再次只适用于 G3 和模拟器。
代码:
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);
}
}
}
String mCurrentPhotoPath;
Bitmap bitmapImg;
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 = image.getAbsolutePath();
return image;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
/*Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
mImageView.setImageBitmap(imageBitmap);*/
bitmapImg = BitmapFactory.decodeFile(mCurrentPhotoPath);
mImageView.setImageBitmap(bitmapImg);
}
}
@Override
public void onClick(View view) {
dispatchTakePictureIntent();
}
同样如前所述,数据 Intent 仅在三星手机上为空。S5 关于相机或权限有什么不同吗?我很想得到一些帮助,这对我很重要。提前致谢。