English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
この記事では、Androidプログラミングでネットワーク上の画像を表示する方法について説明します。皆さんに共有し、以下のように詳細に説明します:
Androidでネットワーク上の画像を表示するには、まずURLに基づいて画像のアドレスを見つけ、その画像をJavaのInputStreamに変換し、そのInputStreamをBitMapに変換し、BitMapは直接androidのImageViewに表示できます。これがネットワーク上の画像を表示する方法の考え方で、実装は非常に簡単です。以下に実装のプロセスを見てみましょう。
まずAndroidManifest.xmlにプログラムにインターネットアクセスの権限を追加します:
<uses-permissionandroid:name="android.permission.INTERNET" />
その後、レイアウトファイルにImageViewを追加し、ネットワーク上の画像を表示します:
<?xml version="1.0" encoding="utf-8"?> <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:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@"+id/imageView" /> </LinearLayout>
メインのActivityでネットワークから画像を取得し、InputStreamに変換し、ImageViewに表示できるBitmapに変換します。
package com.image; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.widget.ImageView; public class NetImageActivity extends Activity { /** Called when the activity is first created. */ String imageUrl = "http://content.52pk.com/files/100623/2230_102437_1_lit.jpg"; Bitmap bmImg; ImageView imView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); imView = (ImageView) findViewById(R.id.imageView); imView.setImageBitmap(returnBitMap(imageUrl)); } public Bitmap returnBitMap(String url){ URL myFileUrl = null; Bitmap bitmap = null; try { myFileUrl = new URL(url); } catch (MalformedURLException e) { e.printStackTrace(); } try { HttpURLConnection conn = (HttpURLConnection) myFileUrl .openConnection(); conn.setDoInput(true); conn.connect(); InputStream is = conn.getInputStream(); bitmap = BitmapFactory.decodeStream(is); is.close(); } catch (IOException e) { e.printStackTrace(); } return bitmap; } }
それから、プログラムを実行すると、ネットワーク上の画像が表示されます。
実行結果:
PS:AndroidManifest.xmlの権限制御の詳細については、このサイトのオンラインツールを参照してください:
Android Manifest機能と権限説明大全:
http://tools.jb51.net/table/AndroidManifest
Androidに関する詳細な内容に興味がある読者は、以下のサイトの特集をチェックしてください:《Android グラフィックスと画像処理の技術まとめ》、《Android 開発入門と上級教程》、《Android デバッグ技術と一般的な問題解決方法のまとめ》、《Android メディア操作技術まとめ(オーディオ、ビデオ、録音など)》、《Android 基本コンポーネントの使用方法まとめ》、《Android ビュー View 技術まとめ》、《Android レイアウト layout 技術まとめ》および《Android コントロールの使用方法まとめ》
この記事で述べたことが皆様のAndroidプログラムデザインに役立つことを願っています。
声明:この記事の内容はインターネットから取得しており、著作権者に帰属します。インターネットユーザーが自発的に貢献し、自己でアップロードしたものであり、このサイトは所有権を有しておらず、人工的な編集は行われていません。著作権に関する問題があれば、以下のメールアドレスにご連絡ください:notice#oldtoolbag.com(メールを送信する際、#を@に置き換えてください。報告を行い、関連する証拠を提供してください。一旦確認が取れたら、このサイトは即座に侵害を疑われるコンテンツを削除します。)