我想从url.i加载一个图像在我的服务器上有10个图像,我在每个类中调用一个。它可以工作,但有些图片没有打开...我的代码有什么问题吗?我检查了网址,确定了!谢谢
public class modern_clock extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.sites);
ImageView im = (ImageView) findViewById(R.id.image);
Drawable drawable = LoadImageFromWeb("http://kostas-menu.gr/chania/roloi.jpg");
im.setImageDrawable(drawable);
TextView title1 = (TextView) findViewById(R.id.text_title);
title1.setText("The Clock\n" );
TextView info = (TextView) findViewById(R.id.info);
info.setText(".................\n" );
}
private Drawable LoadImageFromWeb(String url) {
// TODO Auto-generated method stub
try{
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
}catch (Exception e) {
System.out.println("Exc="+e);
return null;
}
}
}
编辑:
@George:
我在我的包中添加了LoaderImageView,java class.i也将我的xml文件从简单的imageview改为
<com.example.android.LoaderImageView
android:layout_marginTop="10px"
android:id="@+id/loaderImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
image="http://developer.android.com/images/dialog_buttons.png"
/>
在我的项目的每个课程中,我都补充说:
final LoaderImageView image = new LoaderImageView(this, "http://kostas-menu.gr/chania/santrivani.jpg");
image.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
但不幸的是,通过这种方式,我总是从我的xml文件中的url获取图像。我的所有类都有相同的xml,所以我怎么能从java文件设置图像?我试图擦除线图像:来自清单的“http ....”,但这根本不会返回任何图像!!谢谢
答案 0 :(得分:1)
答案 1 :(得分:0)
使用此资源。我相信这会对你有所帮助
http://www.anddev.org/novice-tutorials-f8/imageview-with-loading-spinner-t49439.html
你使用这段代码吗?这是我们应该做的事吗?public class ImageExampleXML extends Activity {
private final String images[] = {"http://developer.android.com/images/dialog_custom.png", "http://developer.android.com/images/dialog_progress_bar.png"};
private int i = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button loadImage = (Button) findViewById(R.id.button);
final LoaderImageView image = (LoaderImageView) findViewById(R.id.loaderImageView);
loadImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
image.setImageDrawable(getAnImageUrl());
}
});
}
private String getAnImageUrl() {
i++;
if(i >= images.length){
i = 0;
}
return images[i];
}
}