我使用以下代码显示带有下一个和上一个按钮的远程图像。 单击下一个和上一个按钮两到三次它不会显示图像下一个图像。 和DisplayLiveImage的位图为空。
任何一项检查都可以解决这个问题吗?还是连接?
ImageView img;
int CurrentImageIndex;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.imageviwer);
setTitle("some text");
myRemoteImages = new String[6];
myRemoteImages[0]="http://www.comparecheapinsurance.com/car-insurance/images/car-insurance-policy.jpg";
myRemoteImages[1]="http://www.speedace.info/solar_cars/solar_car_images/auburn_university_solar_car_banked_road_test.jpg";
myRemoteImages[2]="http://zedomax.com/blog/wp-content/uploads/2009/07/car.jpg";
myRemoteImages[3]="http://www.wallpaperez.net/wallpaper/car/Lamborghini-prototype-car-926.jpg";
myRemoteImages[4]="http://www.evbeat.com/blog/wp-content/uploads/2009/03/aptera-electric-car.jpg";
myRemoteImages[5]="http://www.cartuningcentral.com/wp-content/uploads/2008/01/exotic-car-pagani-zonda.jpg";
CurrentImageIndex= 0;
img= (ImageView)findViewById(R.id.myImageView);
DisplayLiveImage(CurrentImageIndex);
Button previous = (Button)findViewById(R.id.btnPrevious);
Button next = (Button)findViewById(R.id.btnNext);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
ShowNext();
}
});
previous.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
ShowPrevious();
}
});
}
public void DisplayLiveImage(int val)
{
try {
URL aURL = new URL(myRemoteImages[val]);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
Bitmap bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
// i.setImageBitmap(bm);
img.setImageBitmap(bm);
} catch (IOException e) {
}
// i.setScaleType(ImageView.ScaleType.FIT_CENTER);
// i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
// return i;
}
public void ShowNext()
{
if(CurrentImageIndex < myRemoteImages.length )
{
CurrentImageIndex = CurrentImageIndex +1 ;
DisplayLiveImage(CurrentImageIndex);
}
}
public void ShowPrevious()
{
if(CurrentImageIndex > 0 )
{
CurrentImageIndex = CurrentImageIndex -1 ;
DisplayLiveImage(CurrentImageIndex);
}
}
任何帮助都会得到满足。
答案 0 :(得分:0)
问题可能在于您尝试在图像视图中下载和设置的图像的分辨率/尺寸。
此网址上的图片
http://www.wallpaperez.net/wallpaper/car/Lamborghini-prototype-car-926.jpg
是1920 * 1200像素,这对于模拟器来说可能太大了。该代码适用于其他较小的图片。