我正在尝试将字节数组写入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
任何人都可以帮助我吗?
答案 0 :(得分:0)
将这些权限添加到您的清单
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
答案 1 :(得分:0)
这件事发生在我身上,因为该文件不存在,请尝试添加
file1.createNewFile();
在你开始写作之前。