gpt4 book ai didi

android - Android 中的 NegativeArraySizeException

转载 作者:行者123 更新时间:2023-11-29 00:49:34 33 4
gpt4 key购买 nike

我正在尝试运行此代码,但出现错误。谁能告诉我为什么?调试器显示如下

线程 [<3> main](已暂停(异常 NegativeArraySizeException)) ViewRoot.handleMessage(消息)行:1704 ViewRoot(处理程序).dispatchMessage(消息)行:99 Looper.loop() 行:123 ActivityThread.main(String[]) 行:4203 Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) 行:不可用 [本地方法] Method.invoke(Object, Object...) 行:521 ZygoteInit$MethodAndArgsCaller.run() 行:791 ZygoteInit.main(String[]) 行:549 NativeStart.main(String[]) 行:不可用 [本地方法]

  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;
}


}

最佳答案

我会检查 fullAmrDataLength 是否为负数,因为那时

byte[] amrData = new byte[fullAmrDataLength];

将导致 NegativeArraySizeException

关于android - Android 中的 NegativeArraySizeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4047138/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com