In Android, The Drawable resource ID is stored on the Sdcard, And the drawablesdcard
The process is divided into three steps:
1. Convert resource ID to Drawable
2. Convert Drawable to Bitmap
3. Store Bitmap on 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);}/*** the image whose storage resource is ID * @ param id * @ param name */public void saveDrawableById (int id, String name, Bitmap. compressFormat format) {Drawable drawable = idToDrawable (id); Bitmap bitmap = drawableToBitmap (drawable); saveBitmap (bitmap, name, format );} /*** convert the resource ID to Drawable * @ param id * @ return */public Drawable idToDrawable (int id) {return this. getResources (). getDrawable (R. drawable. p4);}/*** converts Drawable to Bitmap * @ param drawable * @ return */public Bitmap drawableToBitmap (Drawable drawable) {if (drawable = null) return null; return (BitmapDrawable) drawable ). getBitmap ();}/*** save Bitmap to the specified path * @ param bitmap * @ param path */public void saveBitmap (Bitmap bitmap, String name, Bitmap. compressFormat format) {// create a File on the SD card file = new File (Environment. getExternalStorageDirectory (), name); FileOutputStream out = null; try {// open the output stream of the specified file out = new FileOutputStream (file); // output the bitmap to the specified file. compress (format, 100, out); out. close ();} catch (IOException e) {e. printStackTrace ();}}}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.