Android read, write pictures to sd card source code

Source: Internet
Author: User

<pre name= "code" class= "HTML" ><!--<span style= "font-family:arial, Helvetica, Sans-serif;" > Add </span><span style= in the Androidmainfest.xml file "font-family:arial, Helvetica, Sans-serif;" >--></span>
<!--Create and delete files in SDcard permissions--<uses-permission android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS" ></uses-permission> <!--write data to SDcard permissions--<uses-permission android:name= " Android.permission.WRITE_EXTERNAL_STORAGE "></uses-permission> <uses-permission android:name=" Android.permission.RESTART_PACKAGES "></uses-permission>
<!--<span style= "font-family:arial, Helvetica, Sans-serif;" > Note: Before <application...></application> add </span><span style= "font-family:arial, Helvetica, Sans-serif; " >--></span>

/** * Save Bitmap to a file. Saves the picture to the SD card. * * @param bitmap * @param file * @return error message if the saving is failed.     Null if the saving is * successful. * @throws IOException */public static void Savebitmaptofile (Bitmap Bitmap, String _file) throws Ioexcep tion {//_file = <span style= "font-family:arial, Helvetica, Sans-serif;" >getsdpath () + "</span><span style=" font-family:arial, Helvetica, Sans-serif; " >/xx Custom Folders </span><span style= "font-family:arial, Helvetica, Sans-serif;" >/hot.png</span><span style= "font-family:arial, Helvetica, Sans-serif;"        > "</span> bufferedoutputstream os = null;            try {File File = new file (_file);            String _filepath_file.replace (File.separatorchar +//File.getname (), "");            int end = _file.lastindexof (File.separator);            String _filepath = _file.substring (0, end); File FilePath = newFile (_filepath);            if (!filepath.exists ()) {filepath.mkdirs ();            } file.createnewfile ();            OS = new Bufferedoutputstream (new FileOutputStream (file));        Bitmap.compress (Bitmap.CompressFormat.PNG, MB, OS);                } finally {if (OS! = null) {try {os.close ();                } catch (IOException e) {log.e (Tag_error, E.getmessage (), E); }            }        }    }
<pre name= "code" class= "Java" >//read public class Mainact extends Activity {/** Called when the activity is FIR St created.        */Private ImageView img; SD Picture Path private String filepath = <span style= "font-family:arial, Helvetica, Sans-serif;" >getsdpath () + "</span><span style=" font-family:arial, Helvetica, Sans-serif; " >/xx Custom Folders </span><span style= "font-family:arial, Helvetica, Sans-serif;" >/hot.png</span><span style= "font-family:arial, Helvetica, Sans-serif;" > </span> @Override public void onCreate (Bundle savedinstancestate) {super.oncreate                (savedinstancestate);                Setcontentview (R.layout.main);                img = (ImageView) Findviewbyid (r.id.img);                File File = new file (filepath);                        if (file.exists ()) {Bitmap BM = bitmapfactory.decodefile (filepath); Display a picture in ImageView IMg.setimagebitmap (BM); }        }}
/** * Get SDK Path * @return */public static String Getsdpath () {        File sddir = null;        Boolean sdcardexist = Environment.getexternalstoragestate ()                              . Equals (Android.os.Environment.MEDIA_MOUNTED);   Determine if the SD card exists        if   (sdcardexist)          {                                        sddir = environment.getexternalstoragedirectory ();//Get with directory       }          return sddir.tostring ();        }


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.