@@ -0,0 +1,2866 @@
+/*%0A * Copyright 2014 Kevin Quan (kevin.quan@gmail.com)%0A * %0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *     http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0Apackage com.kevinquan.android.utils;%0A%0Aimport java.io.File;%0Aimport java.io.FileWriter;%0Aimport java.io.IOException;%0A%0Aimport android.os.Environment;%0Aimport android.text.TextUtils;%0Aimport android.util.Log;%0A%0A/**%0A * Collection of utilities for working with the SD card%0A * @author Kevin Quan (kevin.quan@gmail.com)%0A *%0A */%0Apublic class SDUtils %7B%0A%0A    private static final String TAG = SDUtils.class.getSimpleName();%0A%0A    /**%0A     * Saves a string representation of an object to the SD card at the provided destination.%0A     * @param destination The relative path from the SD root where the object should be written to.  If any intermediate folders do not exist, they will be created.%0A     * @param content The content to be written to.  The result of the toString() method on the object will be written to disk.%0A     * @return A file object of the destination file, or null if the write did not succeed.%0A     */%0A    public static File saveObjectToSD(String destination, Object content) %7B%0A        if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) %7B%0A            Log.w(TAG, %22Could not write to SD card as media is not ready (mounted).%22);%0A            return null;%0A        %7D%0A        if (TextUtils.isEmpty(destination)) %7B%0A            Log.w(TAG, %22Could not write to SD card as no destination was provided.%22);%0A            return null;%0A        %7D%0A        if (content == null) %7B%0A            Log.w(TAG, %22Could not write to SD card as content to write was empty.%22);%0A            return null;%0A        %7D%0A        File outputFile = new File(Environment.getExternalStorageDirectory(), destination);%0A        FileUtils.ensureParentFoldersCreated(outputFile, true);%0A        FileWriter writer = null;%0A        try %7B%0A            writer = new FileWriter(outputFile);%0A            writer.write(content.toString());%0A            Log.d(TAG, %22Content written to %22+outputFile.getAbsolutePath());%0A            return outputFile;%0A        %7D catch (IOException e) %7B%0A            Log.e(TAG, %22Could not output content to %22+outputFile.getAbsolutePath(), e);%0A            return null;%0A        %7D finally %7B%0A            if (writer != null) %7B%0A                try %7B%0A                    writer.close();%0A                %7D catch (IOException e) %7B%7D%0A            %7D%0A        %7D%0A    %7D%0A%7D%0A

Write arbitrary objects to SD card