写入失败时,Android压缩位图失败:EBADF(错误的文件描述符)

时间:2016-03-29 17:26:46

标签: android linux io

有时(现在我正在努力弄清楚模式),我的bitmap.compress失败并出现以下错误:

java.io.IOException: write failed: EBADF (Bad file descriptor)
   at libcore.io.IoBridge.write(IoBridge.java:498)
   at java.io.FileOutputStream.write(FileOutputStream.java:186)
   at android.graphics.Bitmap.nativeCompress(Native Method)
   at android.graphics.Bitmap.compress(Bitmap.java:1027) 
   <...>
Caused by: android.system.ErrnoException: write failed: EBADF (Bad file descriptor)
   at libcore.io.Posix.writeBytes(Native Method)
   at libcore.io.Posix.write(Posix.java:271)
   at libcore.io.BlockGuardOs.write(BlockGuardOs.java:313)
   at libcore.io.IoBridge.write(IoBridge.java:493)
   ... 11 more
   : ------- write threw an exception

代码看起来很标准:

FileDescriptor fileDescriptor = getMediaFileDescriptor(thumbUri);
OutputStream outputStream = null;
if (fileDescriptor != null) {
    outputStream = new FileOutputStream(fileDescriptor);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 80, outputStream);
}

getMediaFileDescriptor()这样做:

 private FileDescriptor getMediaFileDescriptor(Uri uri) {
    try {
        ParcelFileDescriptor fileDescriptor = mContext.getContentResolver()
                .openFileDescriptor(uri, "w");
        return (fileDescriptor != null) ? fileDescriptor.getFileDescriptor() : null;
    } catch (FileNotFoundException e) {
        <...>
    }
    return null;
}

对此有什么好的解决方法,为什么会发生?还有什么是坏的&#39;文件描述符?

0 个答案:

没有答案