我正在嘗試將字節數組寫入SD卡中的視頻文件,但出現錯誤。我也發佈錯誤的代碼。請給我任何建議,我應該怎麼做。在SD卡中的視頻文件上寫入字節數組
代碼這裏: -
file1 = new File(Environment.getExternalStorageDirectory()+ "/"+System.currentTimeMillis()+".mp4");
final Handler handler1 = new Handler();
Thread r1 = new Thread() {
public void run() {
try {
if (counter == 0) {
fileOutputStream = new FileOutputStream(file1);
counter++;
}
Bitmap bitmap = mediaPlayer.getCurrentFrame();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
final byte[] byteArray = stream.toByteArray();
fileOutputStream.write(byteArray);
fileOutputStream.flush();
fileOutputStream.close();
} catch (NullPointerException nle) {
nle.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
if (handler1 != null) {
handler1.postDelayed(this, 10);
}
}
};
r1.start();
} catch (Exception e) {
e.printStackTrace();
}
錯誤的位置: -
java.io.IOException: write failed: EBADF (Bad file number)
at libcore.io.IoBridge.write(IoBridge.java:455)
at java.io.FileOutputStream.write(FileOutputStream.java:187)
at java.io.OutputStream.write(OutputStream.java:82)
at com.vvdn.android.dvr.SurfaceViewFragment$2.run(SurfaceViewFragment.java:503)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: libcore.io.ErrnoException: write failed: EBADF (Bad file number)
at libcore.io.Posix.writeBytes(Native Method)
at libcore.io.Posix.write(Posix.java:202)
at libcore.io.BlockGuardOs.write(BlockGuardOs.java:197)
at libcore.io.IoBridge.write(IoBridge.java:450)
... 12 more
任何一個能幫助我嗎?
你能展示代碼嗎? –
我也更新了代碼。所以請看看。 –
你可以在這裏有任何併發或線程問題嗎?像櫃檯這樣的變量顯然是全球性的,這有點令人擔憂。 –