1

I've been looking at this site for the past 3 or so hours. How to copy files from 'assets' folder to sdcard?

This is the best I could come up with because I'm only trying to copy one file at a time.

InputStream in = null;
OutputStream out = null;

public void copyAssets() {

    try {
        in = getAssets().open("aabbccdd.mp3");
        File outFile = new File(root.getAbsolutePath() + "/testf0lder");
        out = new FileOutputStream(outFile);
        copyFile(in, out);
        in.close();
        in = null;
        out.flush();
        out.close();
        out = null;
    } catch (IOException e) {
        Log.e("tag", "Failed to copy asset file: ", e);
    }

}

private void copyFile(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;
    while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    }
}

I've figured out how to create a file and save a text file. http://eagle.phys.utk.edu/guidry/android/writeSD.html

I would rather save an mp3 file to the sdcard rather than a text file.

When I use this code I provided, I get a text document that same size as the aabbccdd.mp3 file. It does not create a folder and save an .mp3 file. It saves a text document in the root folder. When you open it, I see a whole bunch of chinese letters, but at the top in English I can see the words WireTap. WireTap Pro was the program I used to record the sound so I know the .mp3 is passing through. It's just not creating a folder and then saving a file like the above .edu example.

What should I do?

Community
  • 1
  • 1
losethequit
  • 203
  • 3
  • 17
  • are you sure it's a text document? what are you using to open the file? windows notepad has a well known 'bug' that causes it detect some "text" files as unicode and display everything as "chinese". – Marc B Feb 14 '14 at 04:12

1 Answers1

0

I think you should do something like that -[Note: this i used for some other formats not mp3 but its works on my app for multiple format so i hope it will work for u too.]

  InputStream in  = this.getAssets().open("tmp.mp3"); //give path as per ur app         
  byte[] data = getByteData(in);

Make sure u have the folder already exists on path, if folder is not there it will not save content correctly.

  byteArrayToFile(data , "testfolder/tmp.mp3"); //as per ur sdcard path, modify it.

Now the methods ::

1) getByteData from inputstream -

   private byte[] getByteData(InputStream is) 
   {                        
     byte[] buffer= new byte[1024]; /* or some other number */
     int numRead;
     ByteArrayOutputStream bytes = new ByteArrayOutputStream();
     try{           
        while((numRead = is.read(buffer)) > 0) {
            bytes.write(buffer, 0, numRead);
        }           
        return bytes.toByteArray();
    }
    catch(Exception e)
    { e.printStackTrace(); }        
    return new byte[0];     
   }

2) byteArrayToFile

    public void byteArrayToFile(byte[] byteArray, String outFilePath){      
    FileOutputStream fos;
    try {
        fos = new FileOutputStream(outFilePath);
        fos.write(byteArray);
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }        
     }
Neha
  • 1,548
  • 2
  • 10
  • 13