尝试将文件读入 byte[],但最终得到一个空长度数组

Try to read a file into byte[], but eventually get a null length array

我是 Android 的新手,这也是我第一次在 Whosebug 上提问。如果我没有说清楚,请原谅我糟糕的英语。

我正在尝试将文件转换为 byte[],以便稍后可以将该文件上传到服务器。这是我的代码。

        InputStream io = null;
        ByteArrayOutputStream out = null;
        byte[] buff = null;
        try {
            URL url = new URL(String.valueOf(selectedfilesPaths));
            File file = new File(String.valueOf(selectedfilesPaths));
            int size = Integer.parseInt(String.valueOf(file.length()));
            URLConnection conn = url.openConnection();
            io = conn.getInputStream();
            out = new ByteArrayOutputStream();
            int byteWritten = 0;
            int byteCount = 0;
            buff = new byte[size];
            out.write(buff, byteWritten, byteCount);
        } catch (Exception e) {
            e.printStackTrace();
        }
*        multipartBodyBuilder.addFormDataPart("file", "Android_Flask_" + ".mp4", RequestBody.create(MediaType.parse("video/*mp4"), buff));
        RequestBody postBodyfile = multipartBodyBuilder.build();
        postRequest(postUrl, postBodyfile);

这是我遇到的错误

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.contata.FlaskClient.Uploader, PID: 24319
    java.lang.IllegalStateException: Could not execute method for android:onClick
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
        at android.view.View.performClick(View.java:7160)
        at android.view.View.performClickInternal(View.java:7137)
        at android.view.View.access00(View.java:810)
        at android.view.View$PerformClick.run(View.java:27418)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:221)
        at android.app.ActivityThread.main(ActivityThread.java:7540)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
        at android.view.View.performClick(View.java:7160) 
        at android.view.View.performClickInternal(View.java:7137) 
        at android.view.View.access00(View.java:810) 
        at android.view.View$PerformClick.run(View.java:27418) 
        at android.os.Handler.handleCallback(Handler.java:883) 
        at android.os.Handler.dispatchMessage(Handler.java:100) 
        at android.os.Looper.loop(Looper.java:221) 
        at android.app.ActivityThread.main(ActivityThread.java:7540) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950) 
     Caused by: java.lang.NullPointerException: Attempt to get length of null array
        at okhttp3.RequestBody.create(RequestBody.java:78)
*        at com.contata.FlaskClient.Uploader.MainActivity.connectServer(MainActivity.java:171)
        at java.lang.reflect.Method.invoke(Native Method) 
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) 
        at android.view.View.performClick(View.java:7160) 
        at android.view.View.performClickInternal(View.java:7137) 
        at android.view.View.access00(View.java:810) 
        at android.view.View$PerformClick.run(View.java:27418) 
        at android.os.Handler.handleCallback(Handler.java:883) 
        at android.os.Handler.dispatchMessage(Handler.java:100) 
        at android.os.Looper.loop(Looper.java:221) 
        at android.app.ActivityThread.main(ActivityThread.java:7540) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950) 
E/MQSEventManagerDelegate: failed to get MQSService.

第 171 行是 multipartBodyBuilder.addFormDataPart("file", "Android_Flask_" + ".mp4", RequestBody.create(MediaType.parse("video/*mp4"), buff));

selectedfilesPaths 包含文件的 Uri。 似乎 buff[] 没有从 InputStream

那里得到任何东西

我尝试了将文件转换为 byte[] 的不同方法,但仍然无法处理它。总是在同一个地方得到同样的错误。 也许其他原因导致了这个问题? 非常感谢。

==================已编辑================

我更改了一些行的顺序,这样我就不必将 buff[] 初始化为 null。现在应用程序不会崩溃。但是我的服务器只收到一个 0kb 的空文件。还有什么可以尝试的?

        InputStream io = null;
        ByteArrayOutputStream out = null;
        File file = new File(String.valueOf(selectedfilesPaths));
        int size = Integer.parseInt(String.valueOf(file.length()));
        byte[] buff = new byte[size];
        try {
            URL url = new URL(String.valueOf(selectedfilesPaths));
            URLConnection conn = url.openConnection();
            io = conn.getInputStream();
            out = new ByteArrayOutputStream();
            int byteWritten = 0;
            int byteCount = 0;
            out.write(buff, byteWritten, byteCount);
        } catch (Exception e) {
            e.printStackTrace();
        }
        multipartBodyBuilder.addFormDataPart("file", "Android_Flask_" + ".mp4", RequestBody.create(MediaType.parse("video/*mp4"), buff));
        RequestBody postBodyfile = multipartBodyBuilder.build();
        postRequest(postUrl, postBodyfile);

大功告成..你只需要将文件数据存储到buff中即可。

InputStream io = null;
//ByteArrayOutputStream out = null;
File file = new File(String.valueOf(selectedfilesPaths));
int size = Integer.parseInt(String.valueOf(file.length()));
byte[] buff = new byte[size];
try {
    URL url = new URL(String.valueOf(selectedfilesPaths));
    URLConnection conn = url.openConnection();
    io = new FileInputStream(file); // added
    //io = conn.getInputStream();
    //out = new ByteArrayOutputStream();
    //int byteWritten = 0;
    //int byteCount = 0;
    //out.write(buff, byteWritten, byteCount);
    io.read(buff, 0, size);
} catch (Exception e) {
     e.printStackTrace();
}
multipartBodyBuilder.addFormDataPart("file", "Android_Flask_" + ".mp4", RequestBody.create(MediaType.parse("video/*mp4"), buff));
RequestBody postBodyfile = multipartBodyBuilder.build();
postRequest(postUrl, postBodyfile);