将网页图像加载到 Android 时黑屏
从网络加载图像时,我遇到黑屏。
我从网络上获取页面,将其转换为画布,然后将其设置为 ImageView。
现在,当我加载它时,图像中出现黑屏。
活动代码:
public class ImageActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView i = (ImageView) findViewById(R.id.imageView1);
try {
String url = "http://www.rotoworld.com/images/headshots/NBA/1372.jpg";
Drawable image = ImageOperations(this, url);
i.setImageDrawable(image);
} catch (Exception ex) {
ex.printStackTrace();
}
i.setMinimumWidth(22);
i.setMinimumHeight(22);
i.setMaxWidth(22);
i.setMaxHeight(22);
}
private Drawable ImageOperations(Context ctx, String url) {
try {
InputStream is = (InputStream) this.fetch(url);
Drawable d = Drawable.createFromStream(is, "src");
return d;
} catch (MalformedURLException e) {
return null;
} catch (IOException e) {
return null;
}
}
public Object fetch(String address) throws MalformedURLException, IOException {
URL url = new URL(address);
Object content = url.getContent();
return content;
}
}
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
When loading an image from the web I am getting a black screen.
I took the page from the web turned it in to a canvas and then set it to the ImageView.
Now when I load it I am getting a black screen in the image.
Activity's code:
public class ImageActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView i = (ImageView) findViewById(R.id.imageView1);
try {
String url = "http://www.rotoworld.com/images/headshots/NBA/1372.jpg";
Drawable image = ImageOperations(this, url);
i.setImageDrawable(image);
} catch (Exception ex) {
ex.printStackTrace();
}
i.setMinimumWidth(22);
i.setMinimumHeight(22);
i.setMaxWidth(22);
i.setMaxHeight(22);
}
private Drawable ImageOperations(Context ctx, String url) {
try {
InputStream is = (InputStream) this.fetch(url);
Drawable d = Drawable.createFromStream(is, "src");
return d;
} catch (MalformedURLException e) {
return null;
} catch (IOException e) {
return null;
}
}
public Object fetch(String address) throws MalformedURLException, IOException {
URL url = new URL(address);
Object content = url.getContent();
return content;
}
}
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 Android 中打开 Activity 时出现黑屏的常见原因是创建 Activity 需要很长时间。
我认为你应该将图像的获取放在 AsyncTask 或类似的任务中。
The usual reason for getting a black screen when opening an activity in android is that it takes to long to create the activity.
I think you should put the fetching of your image in a AsyncTask or similar.
试试这个...
Try this...
试试这个..它可能对你有帮助
Try this..it may helpful for you
这是最后是如何做到的...
需要使用 Asynctask 我还将 html 发送到网页以便从网络检索实际的 jpg
Here is how did it at the end...
Needed to use the Asynctask I also sen the html to a web page in order to retrieve the actual jpg from the web