gpt4 book ai didi

java - Android Studio - Java 方法调用 getBytesFromFile() 失败

转载 作者:太空宇宙 更新时间:2023-11-04 13:27:33 24 4
gpt4 key购买 nike

我正在使用 Android Studio 和 Oracle Java 8。我试图从文件中获取所有字节并将它们传递给字节数组。下面的代码就像没有看到 import java.io.File;

我收到错误消息:

 cannot resolve method getBytesFromFile(java.io.File)

代码

import java.io.File;

// ...

File path = new File(
Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/zTest-Records/");

path.mkdirs();
try {
recordingFile = File.createTempFile("recording", ".pcm", path);
} catch (IOException e) {
throw new RuntimeException("Couldn't create pcm file", e);
}

// NOTE: The code below gives error message: cannot resolve method 'getBytesFromFile(java.io.File)'
byte[] data = getBytesFromFile(recordingFile);

最佳答案

该函数未定义,也许您从某处复制粘贴此代码。

快速谷歌搜索指向此链接:

https://code.google.com/p/picturesque/source/browse/myClasses/GetBytesFromFile.java?r=1d9332c4c969b4d35847c10f7c83b04c1ccb834f

package myClasses;
import java.util.*;
import java.io.*;

public class GetBytesFromFile {

public static byte[] getBytesFromFile(File file) throws IOException {
InputStream is = new FileInputStream(file);

// Get the size of the file
long length = file.length();

// You cannot create an array using a long type.
// It needs to be an int type.
// Before converting to an int type, check
// to ensure that file is not larger than Integer.MAX_VALUE.
if (length > Integer.MAX_VALUE) {
// File is too large
}

// Create the byte array to hold the data
byte[] bytes = new byte[(int)length];

// Read in the bytes
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead = is.read(bytes, offset, Math.min(bytes.length - offset, 512*1024))) >= 0) {
offset += numRead;
}

// Ensure all the bytes have been read in
if (offset < bytes.length) {
throw new IOException("Could not completely read file "+file.getName());
}

// Close the input stream and return bytes
is.close();
return bytes;
}
}

您可能需要将此类添加到您的项目中

关于java - Android Studio - Java 方法调用 getBytesFromFile() 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32508045/

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