To solve the problem that the system camera program is called through Intent and the image returned is too small, intent calls

Source: Internet
Author: User

To solve the problem that the system camera program is called through Intent and the image returned is too small, intent calls

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,

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.

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.

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.

public static Bitmap 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 map     matrix.postScale(scaleWidth, scaleHeight);     // matrix.postRotate(45);     Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);     bitmap.recycle();     return resizedBitmap;}

  

  

  

  

  

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.