Code for saving images in Android

Source: Internet
Author: User

Copy From camra app:

 

<Br/> Import Java. io. file; <br/> Import Java. io. filenotfoundexception; <br/> Import Java. io. fileoutputstream; <br/> Import Java. io. ioexception; <br/> Import Java. io. outputstream; <br/> Import android. content. contentresolver; <br/> Import android. content. contentvalues; <br/> Import android. graphics. bitmap; <br/> Import android. graphics. bitmap. compressformat; <br/> Import android. location. location; <br/> impor T android. media. exifinterface; <br/> Import android.net. uri; <br/> Import android. OS. environment; <br/> Import android. provider. mediastore. images; <br/> Import android. util. log; <br/> public class imagemanager {<br/> Private Static final string tag = "imagemanager"; <br/> Private Static final URI storage_uri = images. media. external_content_uri; </P> <p> Public static final string camera_image_bucket_name = <Br/> environment. getexternalstoragedirectory (). tostring () <br/> + "/dcim/camera"; <br/> private imagemanager () {<br/>}</P> <p> Private Static Boolean checkfswritable () {<br/> // create a temporary file to see whether a volume is really writeable. <br/> // it's important not to put it in the root directory which may have a <br/> // limit on the number of files. <br/> string directoryname = <br/> Environment. getexternalstoragedirectory (). tostring () + "/dcim"; <br/> file directory = new file (directoryname); <br/> If (! Directory. isdirectory () {<br/> If (! Directory. mkdirs () {<br/> return false; <br/>}< br/> return directory. canwrite (); <br/>}< br/> Public static Boolean hasstorage () {<br/> return hasstorage (true ); <br/>}< br/> Public static Boolean hasstorage (Boolean requirewriteaccess) {<br/> string state = environment. getexternalstoragestate (); <br/> If (environment. media_mounted.equals (State) {<br/> If (requirewriteaccess) {<br/> Boolean writable = checkfswritable (); <br/> return writable; <br/>}else {<br/> return true; <br/>}< br/>} else if (! Requirewriteaccess <br/> & Environment. media_mounted_read_only.equals (State) {<br/> return true; <br/>}< br/> return false; <br/>}</P> <p> Public static int getexiforientation (string filepath) {<br/> int degree = 0; <br/> exifinterface EXIF = NULL; <br/> try {<br/> EXIF = new exifinterface (filepath); <br/>}catch (ioexception ex) {<br/> log. E (TAG, "cannot read EXIF", ex); <br/>}< br/> If (EXIF! = NULL) {<br/> int orientation = EXIF. getattributeint (<br/> exifinterface. tag_orientation,-1); <br/> If (orientation! =-1) {<br/> // We only recognize a subset of orientation tag values. <br/> switch (orientation) {<br/> case exifinterface. orientation_rotate_90: <br/> degree = 90; <br/> break; <br/> case exifinterface. orientation_rotate_180: <br/> degree = 180; <br/> break; <br/> case exifinterface. orientation_rotate_270: <br/> degree = 270; <br/> break; <br/>}< br/> return degree; <br/>}</P> <p> // <Br/> // stores a bitmap or a jpeg byte array to a file (using the specified <br/> // directory and filename ). also Add an entry to the media store for <br/> // this picture. the title, datetaken, location are attributes for the <br/> // picture. the degree is a one element array which returns the orientation <br/> // of the picture. <br/> // <br/> Public static URI addimage (contentresolver C R, String title, long datetaken, <br/> location, string directory, string filename, <br/> bitmap source, byte [] parse data, int [] degree) {<br/> // We shocould store image data earlier than insert it to contentprovider, <br/> // otherwise we may not be able to generate thumbnail in time. <br/> outputstream = NULL; <br/> string filepath = directory + "/" + filename; <br/> try {<B R/> file dir = new file (directory); <br/> If (! Dir. exists () dir. mkdirs (); <br/> file = new file (directory, filename); <br/> outputstream = new fileoutputstream (File); <br/> If (source! = NULL) {<br/> source. compress (compressformat. JPEG, 100, outputstream); <br/> degree [0] = 0; <br/>}else {<br/> outputstream. write (writable data); <br/> degree [0] = getexiforientation (filepath); <br/>}< br/>} catch (filenotfoundexception ex) {<br/> log. W (TAG, ex); <br/> return NULL; <br/>} catch (ioexception ex) {<br/> log. W (TAG, ex); <br/> return NULL; <br/>} finally {<br/> utils. closesilentl Y (outputstream); <br/>}< br/> // read back the compressed file size. <br/> long size = new file (directory, filename ). length (); <br/> contentvalues values = new contentvalues (9); <br/> values. put (images. media. title, title); <br/> // That filename is what will be handed to Gmail when a user shares a <br/> // photo. gmail gets the name of the picture attachment from the <br/> // "display_name" Field. <Br/> values. put (images. media. display_name, filename); <br/> values. put (images. media. date_taken, datetaken); <br/> values. put (images. media. mime_type, "image/JPEG"); <br/> values. put (images. media. orientation, degree [0]); <br/> values. put (images. media. data, filepath); <br/> values. put (images. media. size, size); <br/> If (location! = NULL) {<br/> values. put (images. media. latitude, location. getlatitude (); <br/> values. put (images. media. longpolling, location. getlongpolling (); <br/>}< br/> return Cr. insert (storage_uri, values); <br/>}< br/> 

 

Usage, such as saving a specified view to an image file:

Mainview mmainview = (mainview) findviewbyid (R. id. main_back); </P> <p> If (false = mmainview. isdrawingcacheenabled () {<br/> mmainview. setdrawingcacheenabled (true); <br/>}</P> <p> Bitmap bitmap = mmainview. getdrawingcache (); <br/> // storeimagetofile (Bitmap); <br/> storeimage (bitmap, null); <br/> mmainview. setdrawingcacheenabled (false); </P> <p> toast. maketext (mcontext, "Save photo:" + mlastcontenturi, toast. length_short ). show (); 

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.