使用 getFileDescriptor 加载图像的子采样版本时 EBADF(错误文件编号)
EBADF (Bad file number) while loading subsampled version of image using getFileDescriptor
我试图在单个后台线程中使用 AsyncTask 执行以下代码。它给出 错误文件编号 .
在 google 上搜索后,我发现不止一个线程尝试同时访问或打开以下文件,但在我的场景中,只有一个后台线程 运行 使用 asynctask 。下面的 decodeBitmapFromDescriptor 方法在 SinglTone Class.
中定义
public Bitmap decodeBitmapFromDescriptor(Uri data, int reqWidth, int reqHeight) throws FileNotFoundException {
FileDescriptor fileDescriptor= mContext.getContentResolver().
openFileDescriptor(data, "r").getFileDescriptor();
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
Bitmap bmp=BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);
// it returns null don't know why ?? it works on api level 21
if(bmp==null){
bmp=BitmapFactory.decodeFileDescriptor(fileDescriptor);
}
return bmp;
}
以下是错误的堆栈跟踪。
java.io.IOException: read failed: EBADF (Bad file number) 05-13
11:16:04.652 17881-19663/com.espec.bosleo.espec W/System.err﹕ at
libcore.io.IoBridge.read(IoBridge.java:442) 05-13 11:16:04.652
17881-19663/com.espec.bosleo.espec W/System.err﹕ at
java.io.FileInputStream.read(FileInputStream.java:179) 05-13
11:16:04.652 17881-19663/com.espec.bosleo.espec W/System.err﹕ at
java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:168)
05-13 11:16:04.652 17881-19663/com.espec.bosleo.espec W/System.err﹕
at java.io.BufferedInputStream.read(BufferedInputStream.java:309)
05-13 11:16:04.652 17881-19663/com.espec.bosleo.espec W/System.err﹕
at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
05-13 11:16:04.652 17881-19663/com.espec.bosleo.espec W/System.err﹕
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:527)
05-13 11:16:04.652 17881-19663/com.espec.bosleo.espec W/System.err﹕
at
android.graphics.BitmapFactory.decodeFileDescriptor(BitmapFactory.java:626)
05-13 11:16:04.652 17881-19663/com.espec.bosleo.espec W/System.err﹕
at
android.graphics.BitmapFactory.decodeFileDescriptor(BitmapFactory.java:644)
..................................
注意:以上代码在 Lollipop 版本 API21 上正常工作
我刚刚完成了它。
忘记关闭 ParcelFileDescriptor。
public Bitmap decodeBitmapFromDescriptor(Uri data,
int reqWidth, int reqHeight) throws IOException {
ParcelFileDescriptor ParcelFileDescriptor= mContext.getContentResolver().
openFileDescriptor(data, "r");
FileDescriptor fileDescriptor=ParcelFileDescriptor.getFileDescriptor();
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
Bitmap bmp=BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);
// Here is the solution of my problem
ParcelFileDescriptor.close();
fileDescriptor=null;
// if(bmp==null){
// bmp=BitmapFactory.decodeFileDescriptor(fileDescriptor);
// }
return bmp;
}
我试图在单个后台线程中使用 AsyncTask 执行以下代码。它给出 错误文件编号 .
在 google 上搜索后,我发现不止一个线程尝试同时访问或打开以下文件,但在我的场景中,只有一个后台线程 运行 使用 asynctask 。下面的 decodeBitmapFromDescriptor 方法在 SinglTone Class.
public Bitmap decodeBitmapFromDescriptor(Uri data, int reqWidth, int reqHeight) throws FileNotFoundException {
FileDescriptor fileDescriptor= mContext.getContentResolver().
openFileDescriptor(data, "r").getFileDescriptor();
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
Bitmap bmp=BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);
// it returns null don't know why ?? it works on api level 21
if(bmp==null){
bmp=BitmapFactory.decodeFileDescriptor(fileDescriptor);
}
return bmp;
}
以下是错误的堆栈跟踪。
java.io.IOException: read failed: EBADF (Bad file number) 05-13 11:16:04.652 17881-19663/com.espec.bosleo.espec W/System.err﹕ at libcore.io.IoBridge.read(IoBridge.java:442) 05-13 11:16:04.652 17881-19663/com.espec.bosleo.espec W/System.err﹕ at java.io.FileInputStream.read(FileInputStream.java:179) 05-13 11:16:04.652 17881-19663/com.espec.bosleo.espec W/System.err﹕ at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:168) 05-13 11:16:04.652 17881-19663/com.espec.bosleo.espec W/System.err﹕ at java.io.BufferedInputStream.read(BufferedInputStream.java:309) 05-13 11:16:04.652 17881-19663/com.espec.bosleo.espec W/System.err﹕ at android.graphics.BitmapFactory.nativeDecodeStream(Native Method) 05-13 11:16:04.652 17881-19663/com.espec.bosleo.espec W/System.err﹕ at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:527) 05-13 11:16:04.652 17881-19663/com.espec.bosleo.espec W/System.err﹕ at android.graphics.BitmapFactory.decodeFileDescriptor(BitmapFactory.java:626) 05-13 11:16:04.652 17881-19663/com.espec.bosleo.espec W/System.err﹕ at android.graphics.BitmapFactory.decodeFileDescriptor(BitmapFactory.java:644) ..................................
注意:以上代码在 Lollipop 版本 API21 上正常工作
我刚刚完成了它。 忘记关闭 ParcelFileDescriptor。
public Bitmap decodeBitmapFromDescriptor(Uri data,
int reqWidth, int reqHeight) throws IOException {
ParcelFileDescriptor ParcelFileDescriptor= mContext.getContentResolver().
openFileDescriptor(data, "r");
FileDescriptor fileDescriptor=ParcelFileDescriptor.getFileDescriptor();
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
Bitmap bmp=BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);
// Here is the solution of my problem
ParcelFileDescriptor.close();
fileDescriptor=null;
// if(bmp==null){
// bmp=BitmapFactory.decodeFileDescriptor(fileDescriptor);
// }
return bmp;
}