【发布时间】:2014-02-20 20:03:09
【问题描述】:
我正在尝试将字节数组写入 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
谁能帮帮我?
【问题讨论】:
-
你能显示代码吗?
-
我也更新了代码。所以请看一下。
-
这里有任何并发或线程问题吗?有点令人担忧的是,诸如 counter 之类的变量显然是全局的。
标签: android video bytearray sd-card streamwriter