【发布时间】:2016-04-27 10:06:55
【问题描述】:
我正在开发一个 Android 项目,在该项目中我使用了一个自定义 ListView,左侧有一个图像。
我使用 Genymotion 模拟器,效果很好。图像完美显示:
Screenshot from Genymotion emulator
但是,当我在我的设备(Huawei P8 Lite)上使用该应用程序时,什么都没有显示:Screenshot from device
我试图在我的设备上调试应用程序,图像路径存在(否则它们将是“无可用图像”)答案是:/storage/emulated/0/Pictures/JPEG_20160415_134349_- 350854394.jpg
我去了 Android 设备监视器,图像存在,但路径略有不同(mnt/shell/ 而不是 /storage/,但我认为这不是真正的问题,是吗?)
我在堆栈跟踪中没有任何“OutOfMemory”错误或任何其他错误。
这是我用来显示图片的代码:
if(values.get(position).getImgList().size()==0){
imageView.setImageResource(R.drawable.nopics);
}
else {
//getImg() return the path (string) of the first image in a ArrayList of path
imageView.setImageBitmap(values.get(position).getImg());
}
我很确定 getImg() 会返回一个图像,因为我在将图像发送到 Web 服务时使用了相同的方法。网络服务正确接收图像。
我的 Android 清单中还有读/写内部/外部存储。
编辑:我忘记了布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/icon"
android:layout_width="200px"
android:layout_height="200px"
android:layout_marginLeft="40px"
android:layout_marginRight="10px"
android:layout_marginTop="4px">
</ImageView>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="70px"
android:layout_marginLeft="10px"
android:text="@+id/label"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:textSize="48px" >
</TextView>
<ImageButton
android:layout_width="30dp"
android:layout_height="30dp"
android:id="@+id/croixrouge"
android:src="@drawable/croixrouge"
android:layout_alignBottom="@+id/label"
android:layout_marginBottom="50px"
android:layout_alignEnd="@+id/label" />
<ImageButton
android:layout_width="30dp"
android:layout_height="30dp"
android:id="@+id/option"
android:src="@drawable/modif"
android:layout_alignTop="@+id/croixrouge"
android:layout_toStartOf="@+id/croixrouge"
android:layout_marginRight="5px"/>
<ImageButton
android:layout_width="30dp"
android:layout_height="30dp"
android:id="@+id/refresh"
android:src="@drawable/refreshpurple"
android:layout_alignTop="@+id/option"
android:layout_toStartOf="@+id/option"
android:layout_marginRight="5px" />
</RelativeLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10px"
android:id="@+id/sublabel"
android:text="@+id/sublabel"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:textSize="32px"/>
</LinearLayout>
</LinearLayout>
getImg() 代码:
public Bitmap getImg() {
if(imgList.size() !=0) {
Bitmap bmp = BitmapFactory.decodeFile(imgList.get(0));
return bmp;
}
return null;
}
编辑:我如何选择图像: 受保护的无效onActivityResult(int requestCode,int resultCode, 意图图像数据){ super.onActivityResult(requestCode, resultCode, imageData);
switch (requestCode) {
case Define.ALBUM_REQUEST_CODE:
if (resultCode == RESULT_OK) {
path = imageData.getStringArrayListExtra(Define.INTENT_PATH);
...
}
}
}
后来:
r.setImgList(path);
有人可以帮我理解为什么图像没有显示在真实设备上吗?
【问题讨论】:
-
它不适合在 imageview 中试试这个 android:scaleType="fitXY"
-
模拟器和设备的api等级是多少?
-
scaleType 的东西不起作用,不过谢谢。模拟器和设备都是 API 21,设备在 android 5.0.1 上运行,模拟器在 5.0.0 上运行
-
getImg()返回什么? -
@KNeerajLal 图片的路径,正如我在帖子中所写的那样,/storage/emulated/0/Pictures/JPEG_20160415_134349_-350854394.jpg
标签: java android image emulation device