Android call system camera take photos save photo very small solution

Source: Internet
Author: User

To save a picture small general steps:

1. Call the System camera

New

2. Save Photos

@Override
protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
TODO auto-generated Method Stub
Super.onactivityresult (Requestcode, ResultCode, data);
System.out.println ("Onactivityresult start");

if (ResultCode = = ACTIVITY.RESULT_OK) {
System.out.println ("enter");
String sdstatus = Environment.getexternalstoragestate ();
if (!sdstatus.equals (environment.media_mounted)) {//Detect if SD is available
LOG.I ("Testfile",
"SD card isn't avaiable/writeable right now.");
System.out.println ("SD card isn't avaiable/writeable right now.");
Return
}
String name = System.currenttimemillis () + ". jpg";;
Toast.maketext (this, name, Toast.length_long). Show ();
Bundle bundle = Data.getextras ();
Bitmap Bitmap = (Bitmap) bundle.get ("Data");//Get the data returned by the camera and convert it to Bitmap picture format

FileOutputStream B = null;
File File = new file ("/mnt/sdcard/dcim/camera/");
File.mkdirs ();//Create Folder
System.out.println ("Mkdirs");
String fileName = "/mnt/sdcard/dcim/camera/" +name;

try {
b = new FileOutputStream (fileName);
Bitmap.compress (Bitmap.CompressFormat.JPEG, +, b);//write data to file
} catch (FileNotFoundException e) {
E.printstacktrace ();
} finally {
try {
B.flush ();
B.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}

The cause of the small picture, the data taken from the return value of the photo is already compressed, to be uncompressed we can specify where to save the photo when calling the system camera

Private String Camera_path = Environment.getexternalstoragedirectory (). toString () + "/photo_lj/";//Photo save location

Private String camera_photo_name;//the saved name




New Intent (mediastore.action_image_capture); New File (Camera_path); if (! path1.exists ()) { path1.mkdirs (); } = System.currenttimemillis () + ". jpg"; New File (path1, camera_photo_name); // Moutputfileuri = uri.fromfile (file); Intent.putextra (mediastore.extra_output, Uri.fromfile (file)); 1);

Overloaded methods

@Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {        Super. Onactivityresult (Requestcode, ResultCode, data); Switch(requestcode) { Casetake_picture:if(ResultCode = =RESULT_OK)String filename = camera_path + "/" +Camera_photo_name; Bitmap BM=compressimagefromfile (filename); Imageitem Takephoto=NewImageitem ();                Takephoto.setbitmap (BM);            Bimp.tempSelectBitmap.add (Takephoto); }             Break; }

It's a big picture, and we'd better compress it to show it.

PrivateBitmap compressimagefromfile (String srcpath) {bitmapfactory.options newopts=Newbitmapfactory.options (); Newopts.injustdecodebounds=false;//read-only edges, not read contentBitmap Bitmap = Bitmapfactory.decodefile (Srcpath,NULL); Newopts.injustdecodebounds=false; intW =Newopts.outwidth; inth =Newopts.outheight; floatHH = 800f;//        floatWW = 480f;//        intbe = 1; if(W > H && w >ww) { be= (int) (Newopts.outwidth/ww); } Else if(W < h && h >hh) { be= (int) (Newopts.outheight/hh); }        if(Be <= 0) be= 1; Newopts.insamplesize= be;//set the sample rate//newopts.inpreferredconfig = config.argb_8888;//This mode is default and can not be setNewopts.inpurgeable =true;//Both settings are validNewopts.ininputshareable =true;//C. Images are automatically recycled when system memory is not enoughBitmap=bitmapfactory.decodefile (Srcpath, newopts); //return compressbmpfrombmp (bitmap);//The original method calls this method to attempt to compress two times//It's actually ineffective, everyone, despite trying        returnbitmap; }

OK, you can go to the location of the photo to check, it is about 2M size of photos.

Android call system camera take photos save photo very small solution

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.