当我从相机回来时,我的应用程序崩溃了。应用程序在bellow 19 Android版本上运行完美,但在操作系统版本19上无效。我正在使用谷歌nexus 7来运行此应用程序。
我发给你的是我的代码:
cameraButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, IMAGE_CAPTURE);
}
});
我的onActivityResult(下面给出):
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
if (requestCode == IMAGE_CAPTURE) {
Uri fileUri = null;
if(imageReturnedIntent == null || resultCode != RESULT_OK){
Log.d("ResultNotOk", "resultCode"+resultCode);
File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString());
for(File f : imageStorageDir.listFiles()){
if(Uri.fromFile(f).equals(this.fileUri)){
fileUri = this.fileUri;
Log.d("fileUriNew", "fileUriNew:-" +fileUri);
break;
}
}
}else if (resultCode == RESULT_OK) {
Log.d("ResultOk!!", "resultCode"+resultCode);
fileUri = imageReturnedIntent.getData();
}
//fileUri = imageReturnedIntent.getData();
Log.d("Uri", "FileUri"+fileUri);
Intent cameraIntent = new Intent(this, EditImage.class);
cameraIntent.setType("/*image");
cameraIntent.setData(imageReturnedIntent.getData());
cameraIntent.putExtra("EditMode", true);
cameraIntent.putExtra("Uri", fileUri);
Log.d("imageReturnedIntent", "imageReturnedIntent"+ imageReturnedIntent);
//if edit mode is 1, intent is from camera
cameraIntent.putExtra("EditMode", false);
startActivity(cameraIntent);
}
答案 0 :(得分:2)
cameraButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String fileName = "Camera_Example.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 cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, IMAGE_CAPTURE);
}
});
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (imageUri != null) {
outState.putString("cameraImageUri", imageUri.toString());
}
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
if (savedInstanceState.containsKey("cameraImageUri")) {
imageUri = Uri
.parse(savedInstanceState.getString("cameraImageUri"));
}
}