Android startCamera gives me null Intent and ... does it destroy my global variable?
I have the next problem:
when I try to start my camera, I can take the picture, even save it on my sdcard, but when I'm going to get the path for showing it on my device I get errors.
My global variables are 2 (I used 1 but 2 are better for making sure it's a strange error):
private File photofile;
private Uri mMakePhotoUri;
and this is my start-camera function:
@SuppressLint("SimpleDateFormat")
public void farefoto(int num){
// For naming the picture
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
String n = sdf.format(new Date());
String fotoname = "Immagine-"+ n +".jpg";
//Going through files and folders
File photostorage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File photostorage2 = new File(photostorage, "Immagini");
System.out.println(photostorage+"\n"+photostorage2);
photostorage2.mkdirs();
// My file (global)
photofile = new File(photostorage2, fotoname);
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //intent to start camera
// My URI (global)
mMakePhotoUri = Uri.fromFile(photofile);
new Bundle(); // I took this code from internet, but if I remove this line, it's the same
i.putExtra(MediaStore.EXTRA_OUTPUT, mMakePhotoUri);
startActivityForResult(i, num); //num would be 1 on calling function
}
and my activityresult:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (requestCode == 1){
try{ // tring my global URI
photo = f.decodeAndResizeFile(new File(mMakePhotoUri.getPath()));
}
catch(NullPointerException ex){
System.out.println("fail");
ex.printStackTrace();
try{ // Trying my global FILE
photo = BitmapFactory.decodeFile(photofile.getAbsolutePath());
} catch (Exception e){
e.printStackTrace();
Toast.makeText(this, "C'è stato un errore. Riprova a scattare la foto.", Toast.LENGTH_LONG).show();
}
......
......
.......
}
Always getting NullPointerException
But... if I take the picture again, IT WORKS!!.
I've read everything I could here... but it doesn't have logic when I modify a global variable and I cannot take it again...
Thanks in advance. Cheers.
SOLUTION
As Alex Cohn said, my problem was that I was calling onCreate
before onActivityResult
because of a probably push out of memory (because sometimes doesn't do it), so I wanted to have my app "healthy" and I tried some try / catch
and so I get the data even if it's calling onCreate
or onActivityResult
for the first call, and I wrote that data in a Bundle like explained in the link of restoring our state.
Thanks!.
It's possible that launching of ACTION_IMAGE_CAPTURE
will push your activity out of memory. You should check (I'd simply a log, debugger may have its own side effects) that onCreate()
of your activity is called before onActivityResult()
. If this is the case, you should prepare your activity to reinitialize itself, probably using onSaveInstanceState(Bundle)
.
Note that the decision whether to shut down the activity, or keep it in background, depends on the overall system state that is beyond your control. It won't surprise me if the decision when you take the first picture, is "shut him down!", but when you take picture again, it is "keep him in background".
It doesnot destroy the variables. But after So many days of research LOL I have got a solution of my mistake. When I put the debugger the method calls it like after taking a picture.
- onCreate()
- onActivityResult()
- onCeate()
- onResume()
Its fixed by just put these following lines in t the menifest. It happens due to camera config changes & window soft input mode.
<activity
android:name="packageName.Activity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize" >