我搜索并尝试了几种不同的方法。我两个都很缺乏。这是我目前的方法:
package com.dop.mobilevforum;
import java.io.InputStream;
import java.net.URL;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.VideoView;
public class Vforum extends Activity
{
private String imgPath = "http://mysite.com/mv/vfdemo1/slides/slide1.jpg";
private ImageView slideHolder;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.vforum);
slideHolder = (ImageView) findViewById(R.id.slideHolder);
Drawable drawable = LoadImageFromWebOperations(imgPath);
slideHolder.setImageDrawable(drawable);
}
private Drawable LoadImageFromWebOperations(String url)
{
try
{
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
}
catch (Exception e)
{
Log.w("LoadImageFromWebOperations",e.toString());
return null;
}
}
}
LoadImageFromWebOperations方法返回一个位图,所以我知道该部分正在运行。我认为它失败了slideHolder.setImageDrawable(drawable);
。这是我的XML:
<?xml version="1.0" encoding="UTF-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/mobile_vforum_bg">
<ImageView
android:id="@+id/slideHolder"
android:layout_width="320px"
android:layout_height="240px">
</ImageView>
</FrameLayout>
任何想法?我只是得到一个黑屏,没有错误。
答案 0 :(得分:2)
您最好的选择是使用WebView,但如果您真的想这样做,请不要使用setImageDrawable
。您应该使用setImageBitmap
。如果您的LoadImageFromWebOperations
返回一个位图,那么您不应该更改任何内容,而是您正在使用的功能,它应该可以顺利运行。
答案 1 :(得分:0)
您的代码有效,亲爱的朋友,您的网址无效http://mysite.com/mv/vfdemo1/slides/slide1.jpg。
您可以使用它100%工作,但您的代码也可以使用
URL url = new URL("http://variable3.com/files/images/email-sig.jpg");
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
slideHolder.setImageBitmap(bmp);