由setImageBitmap()设置时,ImageView不会显示图像 [英] ImageView won't show image when set by setImageBitmap()
问题描述
我在显示sdcard上的现有图像时遇到问题。
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());
照片确实存在且尺寸显示为显示,但ImageView标签内没有任何内容
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.
我在SO上发现了一些关于这个问题的帖子,但都没有回答过。
I've seen a few posts on SO regarding this issue, but none of them have yet been answered.
任何想法?
**更新
如果我直接加载文件,而不是通过文件流工作
** 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);
但是如果我然后添加 Bitmap.createScaledBitmap()
方法调用它不再有效,图像不会显示。
but if I then add the Bitmap.createScaledBitmap()
method call it no longer works and the image is not displayed.
推荐答案
我用Bitmap替换了文件流scaledPhoto = BitmapFactory.decodeFile(/ mnt / sdcard /+ Session.PHOTO_FILE_NAME);它现在有效(更新中提到的内容)
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)
这篇关于由setImageBitmap()设置时,ImageView不会显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!