ANDROID_ camera Camera_ Call system camera return data null

Source: Internet
Author: User

this blog post for the original, reproduced please specify the source! http://blog.csdn.net/zimo2013/article/details/169162791. Call the System camera

[Java] view plaincopy

  1. Instantiate a intent and specify the action
  2. Intent Intent = new Intent (mediastore.action_image_capture);
  3. Specifies a file object that corresponds to a picture path
  4. URI = Uri.fromfile (Imageutil.getimagefile ());
  5. Intent.putextra (Mediastore.extra_output, URI);
  6. Start activity
  7. Startactivityforresult (Intent, Request_code_camera);
but the data obtained in Onactivityresult (int requestcode, int resultcode, Intent data) is always null?
2. Cause analysis

So I looked at the Android framework camera application and found out about how the system camera handles the return value data problem!

By default, you do not need to specify Intent.putextra (Mediastore.extra_output, URI); The camera has its own default storage path, and the captured photo will return a thumbnail image. If you want to access the original picture, you can get the original picture position via dat extra. That is, if you specify a target uri,data there is no data, and if you do not specify a URI, then data will be returned ! Now think about it, this design is still very reasonable!

[Java]View Plaincopy
  1. @Override
  2. protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
  3. switch (requestcode) {
  4. Case Request_code_camera:
  5. if (ResultCode = = RESULT_OK) {
  6. if (data! =null) { //may not have specified Intent.putextra (mediastore.extra_output, URI);
  7. //Returns a thumbnail image
  8. if (Data.hasextra ("Data")) {
  9. Bitmap thumbnail = Data.getparcelableextra ("data");
  10. //Get bitmap after the operation
  11. }
  12. }else{
  13. //Because the target URI is specified, stored in the target Uri,intent.putextra (Mediastore.extra_output, URI);
  14. //through the target URI, find the picture
  15. //Zoom processing of the picture
  16. //Operation
  17. }
  18. }
  19. }
  20. }

3.Android System Camera Part key source

[Java]View Plaincopy
  1. First handle the no crop case-just return the value. If the
  2. CALLER specifies a "save Uri" then write the data to it ' s
  3. Stream. Otherwise, pass back a scaled down version of the bitmap
  4. directly in the extras.
  5. if (Msaveuri! = null) { //exists Msaveuri, which specifies the destination URI
  6. OutputStream outputstream = null;
  7. try {
  8. OutputStream = Mcontentresolver.openoutputstream (Msaveuri);
  9. Outputstream.write (data);
  10. Outputstream.close ();
  11. Setresult (RESULT_OK); //return directly to RESULT_OK and do not specify intent
  12. Finish ();
  13. } catch (IOException ex) {
  14. //Ignore exception
  15. } finally {
  16. util.closesilently (OutputStream);
  17. }
  18. } Else {
  19. Bitmap Bitmap = createcapturebitmap (data);
  20. //returns RESULT_OK, and contains a intent object, where extra is data,value as a bitmap
  21. Setresult (RESULT_OK, new Intent ("Inline-data"). PutExtra ("Data", bitmap));
  22. Finish ();
  23. }

ANDROID_ camera Camera_ Call system camera return data null

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.