In Android, The Drawable resource ID is stored on the Sdcard, And the drawablesdcard

Source: Internet
Author: User

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.

Related Article

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.