2013年2月17日 星期日

Saving image to sdcard from drawble resource


Saving image to sdcard from drawble resource:

Say you have an image namely ic_launcher in your drawable. Then get a bitmap object from this image like:
Bitmap bm = BitmapFactory.decodeResource( getResources(), R.drawable.ic_launcher);
The path to SD Card can be retrieved using:
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
Then save to sdcard on button click using:
File file = new File(extStorageDirectory, "ic_launcher.PNG");
outStream
= new FileOutputStream(file);
bm
.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream
.flush();
outStream
.close();
Don't forget to add android.permission.WRITE_EXTERNAL_STORAGE permission.

沒有留言:

張貼留言

Related Posts Plugin for WordPress, Blogger...