Call the camera program and add it to intent.Android. provider. mediastore. extra_outputParameter to set the image storage location. As follows:
Java code
- 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 );
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
- Public class sizedcameraintent extends activity {
- Final Static int camera_result = 0;
- Imageview mimagevview;
- String imagefilepath;
- @ Override
- Protected void oncreate (bundle savedinstancestate ){
- Super. oncreate (savedinstancestate );
- Setcontentview (R. layout. Contents );
- 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 );
- }
- @ Override
- Protected void onactivityresult (INT requestcode, int resultcode, intent data ){
- Super. onactivityresult (requestcode, resultcode, data );
- If (resultcode = result_ OK ){
- Mimagevview = (imageview) findviewbyid (R. Id. returnedimageview );
- Display currentdisplay = getwindowmanager (). getdefadisplay display ();
- Int DW = currentdisplay. getwidth ();
- Int DH = currentdisplay. getheight ();
- // Get the image size but do not load the image
- Bitmapfactory. Options = new bitmapfactory. Options ();
- Options. injustdecodebounds = true;
- Bitmap BMP = bitmapfactory. decodefile (imagefilepath, options );
- Int heightratio = (INT) math. Ceil (options. outheight/(float) DH );
- Int widthratio = (INT) math. Ceil (options. outwidth/(float) dw );
- If (heightratio> 1 & widthratio> 1 ){
- If (heightratio> widthratio ){
- Options. insamplesize = heightratio;
- } Else {
- Options. insamplesize = widthratio;
- }
- }
- // Parse the image
- Options. injustdecodebounds = false;
- BMP = bitmapfactory. decodefile (imagefilepath, options );
- Mimagevview. setimagebitmap (BMP );
- }
- }
- }