Differences between BitmapFactory.decodeResource and BitmapFactory.decodeStream

不想你离开。 提交于 2019-12-12 12:33:45

问题


I've got an if-else scenario where the if-path uses this code:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
options.inPurgeable = true;
options.inInputShareable = true;
bitmap = BitmapFactory.decodeResource(getResources(), resourceId, options);

And the else-path uses this code:

InputStream is = getContentResolver().openInputStream(Uri.fromFile(file));
BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
options.inPurgeable = true;
options.inInputShareable = true;
bitmap = BitmapFactory.decodeStream(is, null, options);

Now to me, reading the short description from the API, testing and Googling I can't quite figure out the difference, but there seems to be a suble one.

The first piece of code scales to fit the width of the screen when applied to a ImageView (within a RelativeLayout, within a ScrollView). The second piece of code does not, which is my problem. It seems to loose it's aspect ratio somehow as well, because if I apply:

imageView.setAdjustViewBounds(true);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);

It manages to fit it to the width, but messes up the height (making it shorter than it should be). Any input would be greatly appreciated!

(I've already tried to change the first piece of code to see if I had messed something else up, but this rewrite of the if-path gave the same error:)

BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
options.inPurgeable = true;
options.inInputShareable = true;
bitmap= BitmapFactory.decodeStream(getResources().openRawResource(resourceId), null, options);

来源:https://stackoverflow.com/questions/10273650/differences-between-bitmapfactory-decoderesource-and-bitmapfactory-decodestream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!