android array contents twice the size when written to a file?
i've an app that records audio samples. once a recording has been made it is stored on sdcard under reversme.pcm. i then can enter a filename and the app creates a file under that name and copies the contents of reveseme.pcm to the new file under the new filename. the problem i'm having is the new file is twice the size of the original, and when i try to play it there is no sound. i've run it through audacity and there is no sound data there. the file size is exactly twice as big though. any ideas? here some code 'code'
File tempFile = new File(Environment.getExternalStorageDirectory().
getAbsolutePath() + "/"+"reverseme.pcm");
File saveFile = new File(Environment.getExternalStorageDirectory().
getAbsolutePath() + "/"+fileNameToSave);
short[] tempArray = new short[(int)tempFile.length()];
Log.i("tempfile length = ", ""+tempFile.length());
Log.i("tempArray length = ", ""+tempArray.length);
try {
// Create a DataInputStream to read the audio data back from the saved file.
InputStream is = new FileInputStream(tempFile);
BufferedInputStream bis = new BufferedInputStream(is);
DataInputStream dis = new DataInputStream(bis);
// Read the file into the music array.
int i = 0;
while (dis.available() > 0) {
//music[musicLength-1-i] = dis.readShort();
tempArray[i] = dis.readShort();
i++;
}
dis.close();
Log.i("tempArray length after read = ", ""+tempArray.length);
// Create a DataOuputStream to write the temp array into the saved file.
FileOutputStream os = new FileOutputStream(saveFile);
BufferedOutputStream bos = new BufferedOutputStream(os);
DataOutputStream dos = new DataOutput开发者_如何学CStream(bos);
Log.i("tempArray length before writing to sdcard = ", ""+tempArray.length);
for (int c = 0; c < tempArray.length; c++){
dos.writeShort(tempArray[i]);
}
Log.i("tempfile length after writing to sdcard = ", ""+tempArray.length);
dos.close();
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
return false;
}
});
}
}
The readShort method is suitable for reading data that were written using writeShort. You can't use it for reading any kind of data. You should use read(byte[]) and write(byte[])
精彩评论