【发布时间】:2017-10-10 08:08:38
【问题描述】:
我的应用程序遇到了一些奇怪的行为:在某些设备上,如三星索尼 Onplus,我的应用程序工作正常,但在某些设备上,如
- 联想 a7000
- 红米x
我的代码是:
private void captureImageFromCamera() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(this.getPackageManager()) != null) {
photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException e) {
e.printStackTrace();
}
if (photoFile != null) {
Uri photoURI;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
photoURI = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider", photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
} else {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
}
startActivityForResult(takePictureIntent, Constant.REQUEST_TAKE_PHOTO);
}
}
}
这里我正在创建图像文件
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
imageFileName = "IMAGE_" + timeStamp + "_";
// File storageDir = this.getExternalFilesDir(Environment.DIRECTORY_PICTURES + "/PhotoGallery/");
File storageDir = this.getExternalFilesDir(Environment.DIRECTORY_PICTURES );
File image = File.createTempFile(
imageFileName,
".jpg",
storageDir
);
mCurrentPhotoPath = image.getAbsolutePath();
Log.d("imageFileName", imageFileName);
Log.d("imageFile", image.toString());
return image;
}
这是我的 OnActivityResult:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == Constant.REQUEST_TAKE_PHOTO && resultCode == Activity.RESULT_OK) {
try {
setPic();
} catch (IOException e) {
e.printStackTrace();
}
}
}
在这里,我通过发送到另一个活动来设置 Pic:
private void setPic() throws IOException {
Intent intent = new Intent(MainActivity.this, EditImageActivity.class);
intent.putExtra("mCurrentPhotoPath", compressImage(mCurrentPhotoPath,MainActivity.this));
intent.putExtra("CalledBy","Camera");
startActivity(intent);
}
还有另一个人面临同样的问题。已经找到答案了。我厌倦了很多关于stackoverflow的问题,但没有一个对我有用, 请让我知道这个问题,为什么在某些设备中可以正常工作而我上面提到的某些设备不能, 任何帮助表示赞赏
【问题讨论】:
-
什么是Android版本的手机会崩溃??
-
嘿@Xenolion 我上面提到的型号和版本是棉花糖,我提供所有运行时权限。
-
在onActivityResult中添加super
-
@AnkitAman 你没明白我的意思我在某些设备上遇到问题 某些设备工作正常。
-
是的,我确实正确添加了提供程序。我在上面提到了设备名称。