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