The Android implementation stores the drawable resource ID on the SDcard

Source: Internet
Author: User

The process is divided into three steps:
1. Convert the resource ID to drawable
2. Convert drawable to bitmap
3. Store the bitmap on the SDcard

The code is as follows:

 Public  class mainactivity extends actionbaractivity {    @Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main); Savedrawablebyid (R.DRAWABLE.P4,"P4.jpg", Bitmap.CompressFormat.JPEG); }/** * Storage resource as ID image * @param ID * @param name * *     Public void Savedrawablebyid(intID, String name, Bitmap.compressformat format) {drawable drawable = idtodrawable (ID);        Bitmap Bitmap = Drawabletobitmap (drawable);    Savebitmap (bitmap, name, format); }/** * Convert resource ID to drawable * @param ID * @return  * *     PublicDrawableidtodrawable(intID) {return  This. Getresources (). getdrawable (R.DRAWABLE.P4); }/** * Convert drawable to bitmap * @param drawable * @return  * *     PublicBitmapDrawabletobitmap(drawable drawable) {if(drawable = =NULL)return NULL;return((bitmapdrawable) drawable). Getbitmap (); }/** * Saves bitmap in the specified format to the specified path * @param bitmap * @param Path * *     Public void Savebitmap(Bitmap Bitmap, String name, Bitmap.compressformat format) {//Create a file located on the SD cardFile File =NewFile (Environment.getexternalstoragedirectory (), name); FileOutputStream out =NULL;Try{//Open the specified file output streamout =NewFileOutputStream (file);//Output bitmap to the specified fileBitmap.compress (format, -, out);        Out.close (); }Catch(IOException e)        {E.printstacktrace (); }    }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The Android implementation stores the drawable resource ID on the SDcard

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.