Android Studio - Java方法調用getBytesFromFile()失敗

[英]Android Studio - Java Method Call getBytesFromFile() Fails


I am using Android Studio and Oracle Java 8. I am trying to get all bytes from a file and pass them to a byte array. The code below acts like it does not see import java.io.File;

我正在使用Android Studio和Oracle Java 8.我試圖從文件中獲取所有字節並將它們傳遞給字節數組。下面的代碼就像它沒有看到import java.io.File;

I get the error message:

我收到錯誤消息:

 cannot resolve method getBytesFromFile(java.io.File)

code

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

1 个解决方案

#1


0  

That function is not defined, perhaps you copy paste this code from somewhere.

該函數未定義,也許您從某處復制粘貼此代碼。

A quick google search points to this link:

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

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

You probably need to add this class to your project

您可能需要將此類添加到項目中


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/09/10/9d351b480ef1bf1c1b6f422744a0cac3.html



 
  © 2014-2022 ITdaan.com 联系我们: