I call camera to take a picture. But I cannot go back to my own original activity after taking the picture. What's the problem? Thank you.
public void addEntry(View view)
{
String EntryName=RegisterName.toString();
Toast.makeText(this, EntryName, Toast.LENGTH_LONG);
Intent addEntryintent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(getFilesDir(),EntryName);
registeryFileUri = Uri.fromFile(file);
addEntryintent.putExtra(MediaStore.EXTRA_OUTPUT, registeryFileUri);
startActivityForResult(addEntryintent,TAKE_PICTURE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TAKE_PICTURE)
{
if (data != null)
{
Toast.makeText(this, "Successfully Registered!", Toast.LENGTH_LONG);
ImageView Registerimage= (ImageView)findViewById(R.id.RegisterPicture);
Registerimage.setImageURI(registeryFileUri);
}
}
}
Answer
Use
appContext.getExternalCacheDir()
and don't forget to mention permissons.On some devices
data
will unfortunately be null inonActivityResult
after calling the camera activity. So you may need to save your state in your activity's variables, and them read them inonActivityResult
. Be sure to save these variables inonSaveInstanceState
and restore them inonCreate
.It took me a while to get it working and I have made several things and finally it works. I can't tell certainly which of the things I did is the solution to the problem, but they all together form the working solution.
There are multiple reasons why the camera activity does not return back. Two major ones are:
path for the new picture is invalid, or non-existing, or it can't be created
application got suspended and saved path get lost.
So here is my code solving all these problems, all together working.
First I created helper class
ImageServices
:The code is partially inspired by developer.android.com and partially by CameraLauncher class of Apache Cordova project.
In my activity the event handler for button to take a picture looks like this:
Method
onActivityResult
does not really contain much, as imageFileUri already points to the existing file and necessary rendering is done inonResume
method, which is called when the activity gets back into foreground:But this is still not sufficient, as
imageFileUri
gets lost as your app gets suspended. And on regular device the chances are close to 100%. So next you need to store the value ofimageFileUri
to instance state:and load it again in - easiest is directly in
onCreate
:So, again, on top of many other solutions presented on this site as well as elsewhere, there are two major differences:
getTempDirectoryPath
inspired by Apache CordovaimageFileUri
to survive suspended applicationAnd now - at least for me - everything works fine.