Android SDCard Check generateLogOnSD(String sBody, String fileName, String folderName)

Here you can find the source of generateLogOnSD(String sBody, String fileName, String folderName)

Description

public static void generateLogOnSD(String sBody, String fileName, String folderName)

this method can be used to write file on sd card.

License

Apache License

Parameter

Parameter Description
sBody Content of the string
fileName File name
folderName Folder Name

Declaration

public static void generateLogOnSD(String sBody, String fileName,
        String folderName) 

Method Source Code

//package com.java2s;
/*/*from  www .j  a  v  a 2 s  . c om*/
 * Copyright 2013 Nagendra K Srivastava.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import android.os.Environment;

import android.util.Log;

public class Main {
    public static final String TAG = "Utility";

    /**
     * <b><i>public static void generateLogOnSD(<font color="red"><u>String</u></font> sBody, <font color="red"><u>String</u></font> fileName, <font color="red"><u>String</u></font> folderName)</b></i></p> 
     * this method can be used to write file on sd card. It receives content of file as sbody parameter, name of the file 
     * as fileName parameter and folder name as folderName parameter, First this method checks that sd card is available or not if available then
     * it will write file on external storage which is called sd card   
     * @param sBody        Content of the string
     * @param fileName      File name 
     * @param folderName   Folder Name
     */
    public static void generateLogOnSD(String sBody, String fileName,
            String folderName) {
        try {
            Log.d(TAG, "inside generateLogOnSD");
            if (isExternalStoragePresent()) {
                File file = new File(
                        Environment.getExternalStorageDirectory(),
                        folderName);
                if (!file.exists()) {
                    file.mkdirs();
                }
                File gpxfile = new File(file, fileName);
                FileWriter writer = new FileWriter(gpxfile, true);
                writer.write(sBody);
                writer.flush();
                writer.close();
            }
        } catch (IOException e) {

            e.printStackTrace();

        }
    }

    /**
     * <b><i>private static boolean isExternalStoragePresent()</b></i></p>
     * this method can used to check that external storage(Sd card on device ) is available on device or not
     * @return {@link bool}
     * 
     */
    private static boolean isExternalStoragePresent() {

        boolean mExternalStorageAvailable = false;
        boolean mExternalStorageWriteable = false;
        String state = Environment.getExternalStorageState();

        if (Environment.MEDIA_MOUNTED.equals(state)) {
            mExternalStorageAvailable = mExternalStorageWriteable = true;
        } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
            mExternalStorageAvailable = true;
            mExternalStorageWriteable = false;
        } else {
            mExternalStorageAvailable = mExternalStorageWriteable = false;
        }
        if (!((mExternalStorageAvailable) && (mExternalStorageWriteable))) {
        }
        return (mExternalStorageAvailable) && (mExternalStorageWriteable);
    }
}

Related

  1. isExternalStorageReadable()
  2. isExternalStorageWritable()
  3. hasMountSDCard()
  4. getExternalStorageFreeSize()
  5. getExternalStorageTotalSize()
  6. getDataFilesDir(Context c)
  7. getSDPath()
  8. getSdCacheDir(Context context)
  9. getSdcardFilesDir(Context c)