开发者

BitmapFactory.decodeFileDescriptor returning null with a valid file descriptor

I've got a problem where BitmapFactory.decodeFileDescript开发者_如何学Pythonor is returning a null bitmap. The file descriptor provided is coming from:

AssetManager.openFd("test.png").getFileDescriptor();

The paths are correct, the file exists,no exceptions are being thrown, and FileDescriptor.Valid() returns true.

Other code samples that I've looked at do not seem to have this problem.

This has me stumped, I don't know how I should proceed from here. Any ideas?


I had the same problem. First,I put FileInputStream.getFD and BitmapFactory.decodeFileDescriptor in different threads,it returns null.

PhotoDecodeTask task = new PhotoDecodeTask();
task.execute(new FileInputStream(filePath).getFD());

private class PhotoDecodeTask extends AsyncTask<FileDescriptor,Integer,Bitmap>{

    @Override
    protected Bitmap doInBackground(FileDescriptor... params) {
        ...
        return BitmapFactory.decodeFileDescriptor(params[0],null, opts);
    }
}

I try to put them in the same thread,i works.But i don't know why.

PhotoDecodeTask task = new PhotoDecodeTask();
task.execute(filePath);

private class PhotoDecodeTask extends AsyncTask<String,Integer,Bitmap>{

    @Override
    protected Bitmap doInBackground(String... params) {
        ...
        FileDescriptor fd = new FileInputStream(params[0]);
        return BitmapFactory.decodeFileDescriptor(fd,null, opts);
    }
}


I had the same problem. I had to change my code as follows:

InputStream imgFile = context.getAssets().open(imagePath);
BitmapFactory.decodeStream(imgFile);

So instead of using BitmapFactory.decodeFileDescriptor, I used decodeStream.


I have encountered such issue recently. After some search, finally this works for me:

FileDescriptor fd = AssetManager.openFd("test.png").createInputStream().getFD();

As a side note, this only works for my new phone (Android 5.0+), but still returns null from decodeFileDescriptor for old phone (Android 4.1.2).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜