Android uses intent to turn on flash when camera is photographed

Source: Internet
Author: User

1. Phenomena

When we call the camera on some apps that use an Android phone, we find that sometimes when we take a photo, the program will flash back, which can have a great impact on the user experience. Currently the project also has a similar photo upload function, when the use of rice 3 mobile phone testing, photo upload function can be used normally, when we change on red rice or some other relatively early or relatively low-end mobile phones, there is a flash back.

1.1 Turning on the intent of the camera
/* mPictureCachePath为当前照相相片的存储路径 */pictureCachePath = Utility.getExternalCacheDir(this)+...;intent.putExtra(MediaStore.EXTRA_OUTPUT,        Uri.fromFile(new File(mPictureCachePath)));startActivityForResult(intent, REQUEST_CODE_TAKE_PHOTO);
1.2onActivityResult receive intent return value and get bitmap
FilefileFile(mPictureCachePath);if (file{                Bitmap bitmap = BitmapUtils.convertToBitmap(            mPictureCachePath, width, height);}
2. Analyzing problems

After debug we can find that if the program has a flashback, the Mpicturecachepath value is null at this point, but we have already assigned a value for this variable at the outset, why is the MPICTURECACHEPATH global variable null when returning from the camera program?

So let's assign an initial value to Mpicturecachepath, and the result is that when the program returns, the value of the variable is not the path we've assigned, but the initial value. In this way, I speculate that the current activity should be recreated when the camera program returns.

Through debugging, we found that if the use of 3 mobile phone, the program does not flash back, the camera program in return to our actvity when the OnCreate method is not executed, and the use of red Rice mobile phone, program Flash back, at this time our actvity executed oncreate, that is, with red rice, When we opened the camera program, our own activity was destroyed! So when the camera program returns to our activity, the activity is just created, and the path record variable we just created for the picture is not actually assigned at this time.

3. How to Solve

Since the variables can be lost, we can record this variable and use SP. If we need to record some other variables in our program, such as some of the more complex classes we have customized, the SP will not be able to meet the needs of the storage.

At this point we can use the following two methods

protectedvoidonSaveInstanceState(Bundle outState)protectedvoidonRestoreInstanceState(Bundle savedInstanceState)

These two methods are not each time the activity is called, we notice that both OnCreate and onrestoreinstancestate have a bundle type parameter, then who came to this parameter? It must have been onsaveinstancestate.

The Onsaveinstancestate method is called when our activity is terminated without our own finish (and the onsaveinstancestate is executed without being recycled). At this point we can use the Outstate parameter to store our data, and when OnCreate or onrestoreinstancestate (only called when the activity is actually recycled), take the data and reassign it to our member variables, so that Even if the activity is destroyed, we can still regain the value of the relevant variable.

    /** * Save Album Parameters * Description: For a low-profile phone, when the camera is turned on, the current activity is recycled, causing the activity to be returned again, the original saved * variable is reinitialized, causing the program to fail to run, resulting in a crash.     The relevant variable information needs to be recorded here, and if the activity exits unexpectedly, the value of these variables can be restored by calling the Onrestoreinstancestate function once again. */    @Override    protected void onsaveinstancestate(Bundle outstate) {Super. Onsaveinstancestate (Outstate); LOG.I (TAG,"Onsaveinstancestate");/ * Save Photo path * /Outstate.putstring ("Mcurrentselectpicturecachepath", Mcurrentselectpicturecachepath); }/** * Restore Album Parameters * /    @Override    protected void onrestoreinstancestate(Bundle savedinstancestate) {Super. Onrestoreinstancestate (Savedinstancestate); LOG.I (TAG,"Onrestoreinstancestate");/ * Restore the current photo storage path * /Mpicturecachepath = savedinstancestate.getstring ("Mcurrentselectpicturecachepath", Mcurrentselectpicturecachepath); ......    }

Thus, the value of the Mpicturecachepath can be saved, and when the program returns, if the activity is not recycled, the Onrestoreinstancestate method is not executed, and if it is recycled, The value of the variable is restored by calling the Onrestoreinstancestate method.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android uses intent to turn on flash when camera is photographed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.