ImageView won't show image when set by setImageBitmap()

孤人 提交于 2019-12-07 00:47:09

问题


I am having problems getting an existing image on the sdcard to display.

ImageView _photoView = (ImageView)findViewById(R.id.img_photo);

File photoFile = new File(Environment.getExternalStorageDirectory(), Session.PHOTO_FILE_NAME);
rawFileInputStream = new FileInputStream(photoFile);
Bitmap origPhoto = BitmapFactory.decodeStream(rawFileInputStream, null, new BitmapFactory.Options());

_photoView.setImageBitmap(origPhoto);
Log.d(TAG, origPhoto.getWidth() + " - " + origPhoto.getHeight());

The photo does exist and the dimensions show up as displayed, but nothing appears within the ImageView tag

<ImageView 
    android:id="@+id/img_photo"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

I tried to set the height to a fixed size, but I still can't see the photo.

I've seen a few posts on SO regarding this issue, but none of them have yet been answered.

Any ideas?

** Update If I load the file directly, instead of through a filestream it works

Bitmap origPhoto = BitmapFactory.decodeFile("/mnt/sdcard/" + Session.PHOTO_FILE_NAME);
double scale = MAX_WIDTH * 1.0 / origPhoto.getWidth();
int height = (int)(origPhoto.getHeight() * scale);
Bitmap scaledPhoto = Bitmap.createScaledBitmap(origPhoto, MAX_WIDTH, height, true);
_photoView.setImageBitmap(origPhoto);

but if I then add the Bitmap.createScaledBitmap() method call it no longer works and the image is not displayed.


回答1:


I replaced the file streaming with Bitmap scaledPhoto = BitmapFactory.decodeFile("/mnt/sdcard/" + Session.PHOTO_FILE_NAME); and it now works (what was mentioned in the Update)



来源:https://stackoverflow.com/questions/6538843/imageview-wont-show-image-when-set-by-setimagebitmap

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