NegativeArraySizeException Android中 [英] NegativeArraySizeException in Android
问题描述
我试图运行此code,却得到一个错误。谁能告诉我为什么吗?调试器显示以下
主题[3;>主](暂停(例外NegativeArraySizeException))
ViewRoot.handleMessage(消息)行:1704
的ViewRoot(处理器).dispatchMessage(消息)行:99
Looper.loop()行:123
ActivityThread.main(字符串[])行:4203
Method.invokeNative(对象,对象[],上课,下课[],类,整型,布尔)行:不可用[本机方法]
Method.invoke(对象,对象...)线:521
ZygoteInit $ MethodAndArgsCaller.run()行:791
ZygoteInit.main(字符串[])线:549
NativeStart.main(字符串[])行:不可用[本机方法]
公共无效convertAudio()抛出IOException
Log.d(文件,转换音频);
java.io.File的fileExist =新的java.io.File(/ SD卡/,test1.3gp);
java.io.File的fileNew =新的java.io.File(/ SD卡/,test2.3gp);
如果(fileNew.exists()){
的FileInputStream fExist =新的FileInputStream(fileExist);
的FileInputStream fNew =新的FileInputStream(fileNew); 长lengthExist = fileExist.length();
如果(lengthExist> Integer.MAX_VALUE的){
//文件过大
}
字节[] = bytesExist新的字节[(INT)lengthExist] INT偏移= 0;
INT numRead = 0;
尝试{
而(偏移下; bytesExist.length&放大器;及(numRead = fExist.read(bytesExist,胶印,bytesExist.length偏移))GT; = 0){
胶印+ = numRead;
}
} {最后
} //确保所有字节已经阅读
如果(偏移< bytesExist.length){
抛出新IOException异常(无法完全读取文件+ fileExist.getName());
}
字节[] = cleanExist convert3gpDataToAmr(bytesExist); 长lengthNew = fileNew.length();
如果(lengthNew> Integer.MAX_VALUE的){
//文件过大
}
字节[] = bytesNew新的字节[(INT)lengthNew] INT offsetNew = 0;
INT numReadNew = 0;
尝试{
而(偏移下; bytesNew.length&放大器;及(numRead = fNew.read(bytesNew,offsetNew,bytesNew.length-offsetNew))GT; = 0){
offsetNew + = numReadNew;
}
} {最后
} //确保所有字节已经阅读
如果(偏移< bytesNew.length){
抛出新IOException异常(无法完全读取文件+ fileExist.getName());
}
字节[] = cleanNew convert3gpDataToAmr(bytesNew); }
}
//#!AMR \\ n
私有静态的byte [] AMR_MAGIC_HEADER = {0x23,0×21,×41,送出0x4d,0×52,的0x0A}; 公众的byte [] convert3gpDataToAmr(字节[]数据){
如果(数据== NULL){
返回null;
} ByteArrayInputStream的双=新ByteArrayInputStream的(数据);
//读取FileTypeHeader
FileTypeBox ftypHeader =新FileTypeBox(之二);
//您可以检查它是否是正确的在这里
//读取MediaDataHeader
MediaDataBox mdatHeader =新MediaDataBox(之二);
//您可以检查它是否是正确的在这里
INT rawAmrDataLength = mdatHeader.getDataLength();
INT fullAmrDataLength = AMR_MAGIC_HEADER.length + rawAmrDataLength;
字节[] = amrData新的字节[fullAmrDataLength]
System.arraycopy(AMR_MAGIC_HEADER,0,amrData,0,AMR_MAGIC_HEADER.length);
bis.read(amrData,AMR_MAGIC_HEADER.length,rawAmrDataLength);
返回amrData;
}
}
我会检查是否 fullAmrDataLength
是否定的,因为那样的话
字节[] = amrData新的字节[fullAmrDataLength]
将导致 NegativeArraySizeException
。
I am trying to run this code, but get an error. Can anyone tell me why? The debugger shows the following
Thread [<3> main] (Suspended (exception NegativeArraySizeException)) ViewRoot.handleMessage(Message) line: 1704 ViewRoot(Handler).dispatchMessage(Message) line: 99 Looper.loop() line: 123 ActivityThread.main(String[]) line: 4203 Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method] Method.invoke(Object, Object...) line: 521 ZygoteInit$MethodAndArgsCaller.run() line: 791 ZygoteInit.main(String[]) line: 549 NativeStart.main(String[]) line: not available [native method]
public void convertAudio () throws IOException {
Log.d("FILE", "Converting Audio");
java.io.File fileExist = new java.io.File("/sdcard/" , "test1.3gp");
java.io.File fileNew = new java.io.File("/sdcard/" , "test2.3gp");
if (fileNew.exists()) {
FileInputStream fExist = new FileInputStream(fileExist);
FileInputStream fNew = new FileInputStream(fileNew);
long lengthExist = fileExist.length();
if (lengthExist > Integer.MAX_VALUE) {
// File is too large
}
byte[] bytesExist = new byte[(int)lengthExist];
int offset = 0;
int numRead = 0;
try {
while (offset < bytesExist.length && (numRead=fExist.read(bytesExist, offset, bytesExist.length-offset)) >= 0) {
offset += numRead;
}
} finally {
}
// Ensure all the bytes have been read in
if (offset < bytesExist.length) {
throw new IOException("Could not completely read file "+fileExist.getName());
}
byte[] cleanExist = convert3gpDataToAmr(bytesExist);
long lengthNew = fileNew.length();
if (lengthNew > Integer.MAX_VALUE) {
// File is too large
}
byte[] bytesNew = new byte[(int)lengthNew];
int offsetNew = 0;
int numReadNew = 0;
try {
while (offset < bytesNew.length && (numRead=fNew.read(bytesNew, offsetNew, bytesNew.length-offsetNew)) >= 0) {
offsetNew += numReadNew;
}
} finally {
}
// Ensure all the bytes have been read in
if (offset < bytesNew.length) {
throw new IOException("Could not completely read file "+fileExist.getName());
}
byte[] cleanNew = convert3gpDataToAmr(bytesNew);
}
}
// #!AMR\n
private static byte[] AMR_MAGIC_HEADER = {0x23, 0x21, 0x41, 0x4d, 0x52, 0x0a};
public byte[] convert3gpDataToAmr(byte[] data) {
if (data == null) {
return null;
}
ByteArrayInputStream bis = new ByteArrayInputStream(data);
// read FileTypeHeader
FileTypeBox ftypHeader = new FileTypeBox(bis);
// You can check if it is correct here
// read MediaDataHeader
MediaDataBox mdatHeader = new MediaDataBox(bis);
// You can check if it is correct here
int rawAmrDataLength = mdatHeader.getDataLength();
int fullAmrDataLength = AMR_MAGIC_HEADER.length + rawAmrDataLength;
byte[] amrData = new byte[fullAmrDataLength];
System.arraycopy(AMR_MAGIC_HEADER, 0, amrData, 0, AMR_MAGIC_HEADER.length);
bis.read(amrData, AMR_MAGIC_HEADER.length, rawAmrDataLength);
return amrData;
}
}
I would check if fullAmrDataLength
is negative, because then
byte[] amrData = new byte[fullAmrDataLength];
would result in NegativeArraySizeException
.
这篇关于NegativeArraySizeException Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!