正是标题所说的。我正在尝试在片段中显示来自 URl 的 imageView,但没有显示任何内容。Android 不会崩溃,布局只是空的。我在清单中启用了互联网权限。
<uses-permission android:name="android.permission.INTERNET" />
Java代码在这里
public class InboxFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.inbox_frag_layout, container, false);
ImageView image = (ImageView)V.findViewById(R.id.imgView);
Drawable drw =LoadImageFromWebOperations("http://i.imgur.com/Svphn.jpg");
image.setImageDrawable(drw);
return V;
}
private Drawable LoadImageFromWebOperations(String strPhotoUrl) {
try {
InputStream is = (InputStream) new URL(strPhotoUrl).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
}catch (Exception e) {
System.out.println("Exc="+e);
return null;
}
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width = "wrap_content"
android:layout_height="wrap_content"
>
<ImageView
android:id="@+id/imgView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</LinearLayout>
更新:改用 webview 修复它。