问题
I have these codes. Camera opened, the picture is taken, but when I click on "ok" button, nothing happened. The only way to get back to previous activity is clicking the "x" button which is not useful for me :). What is the problem? (onActivityResult method is not finished yet.) (I used to use this algorithm with Android.provider.MediaStore.ACTION_IMAGE_CAPTURE intent. there was everything OK. I have no idea why I have problem now.)
public void onClick(View v) {
Intent getCameraImage = new Intent("android.media.action.IMAGE_CAPTURE");
db.open();
Cursor cr = db.getAllRecords();
int count = cr.getCount();
db.close();
File cameraFolder;
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
cameraFolder = new File(android.os.Environment.getExternalStorageDirectory(),"TSFC");
else
cameraFolder= ShowMessagesPage.this.getCacheDir();
if(!cameraFolder.exists())
cameraFolder.mkdirs();
File photo = new File(Environment.getExternalStorageDirectory(), "TSFC/" + (count + 1) + ".jpg");
getCameraImage.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
initialURI = Uri.fromFile(photo);
startActivityForResult(getCameraImage, CAMERA_RESULT);
}});}
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == RESULT_OK) {
Bundle extras = intent.getExtras();
photo = (Uri) extras.get("data");
}
}
回答1:
Ok, Its well known Bug,
Just Add this code line, photo.createNewFile();
Something like,
File photo = new File(Environment.getExternalStorageDirectory(), "TSFC/" + (count + 1) + ".jpg");
photo.createNewFile();
getCameraImage.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
Also don't forget to,
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
And let me know your progress.
回答2:
Use onActivityResult(..)
Method to get back Result Like this Way.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_RESULT) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
Bundle extras = intent.getExtras();
photo = (Uri) extras.get("data");
}
}
}
来源:https://stackoverflow.com/questions/14295906/i-cannot-return-the-previous-activity-with-using-activityforresult