0

So I have looked up several ways to write to an SD card in Android but none seem to actually result in something being written. I have already set the permission in the manifest. My FileIO class I wrote:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintStream;

//import android.app.Activity;
//import android.os.Bundle;
import android.os.Environment;
import android.util.Log;

public class FileIO {

    private static final String TAG = "FileIO";
    private String filename;
    private File sdCard;
    private File dir;

    public FileIO(String path, String filename){
        Log.d(TAG, "Creating new File IO");
        this.filename = filename;
        boolean mExternalStorageAvailable = false;
        boolean mExternalStorageWriteable = false;
        String state = Environment.getExternalStorageState();

        if (Environment.MEDIA_MOUNTED.equals(state)) {
            // We can read and write the media
            Log.d(TAG, "Read and Write OK");
        } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
            // We can only read the media
            Log.w(TAG, "Read only OK");
        } else {
            // Something else is wrong. It may be one of many other states, but all we need
            //  to know is we can neither read nor write
            Log.w(TAG, "Read and Write BLOCKED");
        }
        sdCard = Environment.getExternalStorageDirectory();
        Log.d(TAG, "Writing to file: " + sdCard.getAbsolutePath()+path);
        dir = new File (sdCard.getAbsolutePath()+path);
        if (dir.mkdirs() || dir.isDirectory()) {
            Log.d(TAG, "SUCCESS - Created directory");
        } else {
            Log.d(TAG, "FAILED - Create directory");
        }

    }

public void writeToFile(String s){
    File file = new File(dir,this.filename);
    try {
        FileOutputStream f = new FileOutputStream(file,true); //True = Append to file, false = Overwrite
        OutputStreamWriter osw = new OutputStreamWriter(f);
        osw.write(s);
        osw.flush();
        f.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        System.out.printf("\nFile not found. Make sure to add WRITE_EXTERNAL_STORAGE permission to the manifest");
    } catch (IOException e) {
        e.printStackTrace();
    }   
}
}

And then this is class it is being used in.

import android.app.Activity;
import android.text.format.DateFormat;
import android.widget.TextView;

public class AudioClipLogWrapper implements AudioClipListener
{
    private TextView log;

    private Activity context;

    private double previousFrequency = -1;
    private int previousVolume = -1;
    private int previousMax = -1;

    private FileIO fileIO;

    public AudioClipLogWrapper(TextView log, Activity context)
    {
        this.log = log;
        this.context = context;
        String dateStamp = (DateFormat.format("dd-MM-yyyy-hh-mm-ss", new java.util.Date()).toString());
        fileIO = new FileIO("/Android/data/com.uni.Lab7/files/",String.format("audio-%s.txt",dateStamp));

    }

    @Override
    public boolean heard(short[] audioData, int sampleRate)
    {

        final double freq = ZeroCrossing.calculate(sampleRate, audioData);      
        final int maxAmplitude = AudioUtil.getMaxValue(audioData);
        final double volume = AudioUtil.rootMeanSquared(audioData);


        final StringBuilder message = new StringBuilder();
        if ((((int)volume) > (4 * previousVolume)) && (maxAmplitude > (4 * previousMax)) ) {
            message.append(" Clap!");
        }
        previousVolume = (int) volume;
        previousMax= (int) maxAmplitude;

        fileIO.writeToFile(String.format("%d, %d, %f\n",(int)volume, maxAmplitude, freq));

        context.runOnUiThread(new Runnable()
        {
            @Override
            public void run()
            {
               AudioTaskUtil.appendToStartOfLog(log, message.toString());
             }
        });

        return false;
    }
}

When I look at the log it suggests everything should work:

01-12 23:21:55.815    2752-2752/com.uni.Lab7 D/FileIO﹕ Creating new File IO
01-12 23:21:55.825    2752-2752/com.uni.Lab7 D/FileIO﹕ Read and Write OK
01-12 23:21:55.825    2752-2752/com.uni.Lab7 D/FileIO﹕ Writing to file: /storage/sdcard0/Android/data/com.uni.Lab7/files/
01-12 23:21:55.835    2752-2752/com.uni.Lab7 D/FileIO﹕ SUCCESS - Created directory

But when I take the SD card out of the phone, and I look on my computer there is no folder and no file. I have looked at Android write to sd card folder and Write a file in SDcard in Android and I have what they suggested.

The phone is a Samsung GT-S53110B running 4.1.2. So in my gradle and manifest I have minimum SDK as 8 and target as 16.

Community
  • 1
  • 1
jnd
  • 754
  • 9
  • 20
  • Are you able to see other files and folders on the SD card? From the question it's ambiguous if the write is failing, if the file is being deleted between your write and when you examine the card, or if your computer isn't able to read the SD card as formatted. – Will Angley Apr 27 '15 at 05:31
  • 2
    Try to `adb shell` in to the device, and navigate to `/sdcard0/Android/data/com.uni.Lab7/files/`, the file is probably there. It's probably getting written to the virtual sdcard. – Daniel Nugent Apr 27 '15 at 05:58
  • I managed to `adb shell` in and yes, they are listed there. So what do i need to do? – jnd Apr 27 '15 at 06:23
  • Your question may be duplicated with this [question][1] . [1]: http://stackoverflow.com/questions/6468805/android-files-that-i-write-to-the-sdcard-do-not-show-in-windows-explorer-for-a – NamNH Apr 27 '15 at 07:04

0 Answers0