Java tutorial
/* 50AH-code ========= 50 Android Hacks (http://manning.com/sessa/) book source code Copyright (c) 2012 Manning Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /******************************************************************************* * Copyright (c) 2012 Manning * See the file license.txt for copying permission. ******************************************************************************/ package com.manning.androidhacks.hack037; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import android.content.Context; import android.os.Environment; import android.util.Log; public final class MediaUtils { private static final String TAG = MediaUtils.class.getCanonicalName(); private MediaUtils() { } public static void saveRaw(Context context, int raw, String path) { File completePath = new File(Environment.getExternalStorageDirectory(), path); try { completePath.getParentFile().mkdirs(); completePath.createNewFile(); BufferedOutputStream bos = new BufferedOutputStream((new FileOutputStream(completePath))); BufferedInputStream bis = new BufferedInputStream(context.getResources().openRawResource(raw)); byte[] buff = new byte[32 * 1024]; int len; while ((len = bis.read(buff)) > 0) { bos.write(buff, 0, len); } bos.flush(); bos.close(); } catch (IOException io) { Log.e(TAG, "Error: " + io); } } }