Solve the Problem of calling the System camera program using intent and returning images too small [Android]

Source: Internet
Author: User
Reposted from hi_android: The final editor hi_android

The following code calls the system photo program,

12 Intent it = newintent ("android. Media. Action. image_capture ");Startactivityforresult (it, activity. default_keys_dialer );

After you press the photo key, your activity will be returned. Therefore, you must add a processing method to the onactivityresult method,

12345678910111213 Protectedvoidonactivityresult (intrequestcode, intresultcode, intent data ){Super. onactivityresult (requestcode, resultcode, data );Try {Bundle extras = data. getextras ();Bitmap B = (Bitmap) extras. Get ("data ");Take = B;Imageview IMG = (imageview) findviewbyid (R. Id. Image );IMG. setimagebitmap (take );} Catch (exception e ){}}

However, you will find that the bitmap is too small. Obviously, the image has been compressed. to return an uncompressed image, you must add a parameter to the System camera program intent to specify the image output position.

123 Intent it = newintent ("android. Media. Action. image_capture ");It. putextra (mediastore. extra_output, Uri. fromfile (newfile (F. sd_card_temp_photo_path )));Startactivityforresult (it, activity. default_keys_dialer );

In this case, the large image is returned.

123456789101112 Protectedvoidonactivityresult (intrequestcode, intresultcode, intent data ){Super. onactivityresult (requestcode, resultcode, data );Try {Imageview IMG = (imageview) findviewbyid (R. Id. Image );Take = U. resizebitmap (U. getbitmapforfile (F. sd_card_temp_photo_path), 640 );IMG. setimagebitmap (take );Imgflag = true;} Catch (exception e ){}}

Note that the returned bitmap will be very large. You need to recycle it after use, or you will easily report an OOM error in the memory.

1234567891011121314151617 Publicstaticbitmap resizebitmap (Bitmap bitmap, intnewwidth ){Intwidth = bitmap. getwidth ();Intheight = bitmap. getheight ();Floattemp = (float) height)/(float) width );Intnewheight = (INT) (newwidth) * temp );Floatscalewidth = (float) newwidth)/width;Floatscaleheight = (float) newheight)/height;Matrix matrix = newmatrix ();// Resize the Bit MapMatrix. postscale (scalewidth, scaleheight );// Matrix. postrotate (45 );Bitmap resizedbitmap = bitmap. createbitmap (bitmap, 0, 0, width, height, matrix, true );Bitmap. Recycle ();Returnresizedbitmap;}
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.