Pro. Android media-process large images

Source: Internet
Author: User

 

Call the camera program and add it to intent.Android. provider. mediastore. extra_outputParameter to set the image storage location. As follows:

 

Java code
 
  1. String imagefilepath = environment. getexternalstoragedirectory (). getabsolutepath ()
  2. + "/Myfavoritepicture.jpg ";
  3. File imagefile = new file (imagefilepath );
  4. Uri imagefileuri = URI. fromfile (imagefile );
  5. // URI imagefileuri = URI. parse ("file: // sdcard/myfavoritepicture.jpg ");
  6. Intent I = new intent (Android. provider. mediastore. action_image_capture );
  7. I. putextra (Android. provider. mediastore. extra_output, imagefileuri );
  8. Startactivityforresult (I, camera_result );
String imagefilepath = environment. getexternalstoragedirectory (). getabsolutepath () + "/myfavoritepicture.jpg"; file imagefile = new file (imagefilepath); Uri imagefileuri = Uri. fromfile (imagefile); // URI imagefileuri = Uri. parse ("file: // sdcard/myfavoritepicture.jpg"); intent I = new intent (Android. provider. mediastore. action_image_capture); I. putextra (Android. provider. mediastore. extra_output, imagefileuri); startactivityforresult (I, camera_result );

 

For large images, OOM (out of memory) may occur during Android loading ). At this time, we need to process the image.

 

Space occupied by images loaded into memory

Bytes -------------------------------------------------------------------------------------------

A 32-bit color image with a resolution of 1024x768. The file size is about?

  

  I. Basic Knowledge

First, we must understand the number of digits occupied by each pixel of the image.

The number of bits in each pixel is roughly as follows.

1 digit, (monochrome); 4 digits: (16 colors); 8 digits (256 colors );

16 (64 K color, high color); 24 (16 M color, true color); 32 (40 96 m color, enhanced true color ).

Then, you must understand that the smallest unit of storage capacity in a computer is bit, that is, bit.

  Ii. Calculation Method

  ① Calculate the number of bits for the image.

When the example is used, the calculation is equal:

A. Total number of pixels: 1024x768 = 786432 pixels

B. Each pixel occupies 32 bits.

Therefore, the total bit size of the image is:

1024x768x32 = 25165824bit

  ② Disk storage space occupied by computing Images

This requires you to have a certain understanding of the conversion of disk storage units. You can use the total number of BITs to convert to the corresponding kb, MB, and GB to calculate the result.

1 byte = 8bit 1kb = 1024b 1 MB = 1024kb

Well, with the above foundation, we can calculate the size of the storage space occupied by the image.

1024x768x32. The current unit is bit.

The current unit of 1024x768x32 bytes 8 is byte.

1024x768x32 bytes 8 bytes 1024 = 3072kb, in KB

1024x768x32 running 8 running 1024 running 1024
= 3 mb. The current unit is MB.

-------------------------------------------------------

It can be seen that the image is loaded into the memory, so we need to process the image.

 

 

Bitmapfactory. Options allows us to control how bitmap is read into memory.

Insamplesize allows us to scale the image

Set injustdecodebounds to true, so that we can get the image size without parsing the image. The returned image object is null. We can get the image information, but we do not need to allocate memory for the image.

 

Java code
 
  1. Public class sizedcameraintent extends activity {
  2. Final Static int camera_result = 0;
  3. Imageview mimagevview;
  4. String imagefilepath;
  5. @ Override
  6. Protected void oncreate (bundle savedinstancestate ){
  7. Super. oncreate (savedinstancestate );
  8. Setcontentview (R. layout. Contents );
  9. Imagefilepath = environment. getexternalstoragedirectory (). getabsolutepath ()
  10. + "/Myfavoritepicture.jpg ";
  11. File imagefile = new file (imagefilepath );
  12. Uri imagefileuri = URI. fromfile (imagefile );
  13. // URI imagefileuri = URI. parse ("file: // sdcard/myfavoritepicture.jpg ");
  14. Intent I = new intent (Android. provider. mediastore. action_image_capture );
  15. I. putextra (Android. provider. mediastore. extra_output, imagefileuri );
  16. Startactivityforresult (I, camera_result );
  17. }
  18. @ Override
  19. Protected void onactivityresult (INT requestcode, int resultcode, intent data ){
  20. Super. onactivityresult (requestcode, resultcode, data );
  21. If (resultcode = result_ OK ){
  22. Mimagevview = (imageview) findviewbyid (R. Id. returnedimageview );
  23. Display currentdisplay = getwindowmanager (). getdefadisplay display ();
  24. Int DW = currentdisplay. getwidth ();
  25. Int DH = currentdisplay. getheight ();
  26. // Get the image size but do not load the image
  27. Bitmapfactory. Options = new bitmapfactory. Options ();
  28. Options. injustdecodebounds = true;
  29. Bitmap BMP = bitmapfactory. decodefile (imagefilepath, options );
  30. Int heightratio = (INT) math. Ceil (options. outheight/(float) DH );
  31. Int widthratio = (INT) math. Ceil (options. outwidth/(float) dw );
  32. If (heightratio> 1 & widthratio> 1 ){
  33. If (heightratio> widthratio ){
  34. Options. insamplesize = heightratio;
  35. } Else {
  36. Options. insamplesize = widthratio;
  37. }
  38. }
  39. // Parse the image
  40. Options. injustdecodebounds = false;
  41. BMP = bitmapfactory. decodefile (imagefilepath, options );
  42. Mimagevview. setimagebitmap (BMP );
  43. }
  44. }
  45. }
Related Article

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.