どのように負荷するImageView URLでスケーラビリティの問題ですね。[定休日]
質問
使い分けが面倒くさくない画像を参照するURLは、 ImageView
?
解決 5
とにかく人が答えとして、それを投稿し、私のコメントをお願いします。私が投稿しています。
URL newurl = new URL(photo_url_str);
mIcon_val = BitmapFactory.decodeStream(newurl.openConnection().getInputStream());
profile_photo.setImageBitmap(mIcon_val);
感謝ます。
他のヒント
<のhref = "からhttp://web.archive.org/web/20120802025411/http://developer.aiwgame.com/imageview-show-image-from-url-on-android-4-0 .htmlを "> Androidデベロッパーするます:
// show The Image in a ImageView
new DownloadImageTask((ImageView) findViewById(R.id.imageView1))
.execute("http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png");
public void onClick(View v) {
startActivity(new Intent(this, IndexActivity.class));
finish();
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
あなたがインターネットにアクセスするために、あなたのAndroidManifest.xml
に設定された次の権限を持っていることを確認します。
<uses-permission android:name="android.permission.INTERNET" />
1. ピカソ でもクレジットカードで簡単画像の読み込みお申し込み—く一行のコード!
利用で、Gradle:
implementation 'com.squareup.picasso:picasso:2.71828'
一行のコード!
Picasso.get().load("http://i.imgur.com/DvpvklR.png").into(imageView);
2. ☆ 画像の読み込みとキャッシュ図書館のためのAndroid心滑らかなスクロール
利用で、Gradle:
repositories {
mavenCentral()
google()
}
dependencies {
implementation 'com.github.bumptech.glide:glide:4.7.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'
}
//簡単なビュー:
Glide.with(this).load("http://i.imgur.com/DvpvklR.png").into(imageView);
3. fresco 強力なシステムに表示する画像スケーラビリティ 願います。フレスコ画の画像の読み込み、表示になります。 ます。
あなたが最初にイメージをダウンロードする必要があります。
public static Bitmap loadBitmap(String url) {
Bitmap bitmap = null;
InputStream in = null;
BufferedOutputStream out = null;
try {
in = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE);
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
copy(in, out);
out.flush();
final byte[] data = dataStream.toByteArray();
BitmapFactory.Options options = new BitmapFactory.Options();
//options.inSampleSize = 1;
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
} catch (IOException e) {
Log.e(TAG, "Could not load Bitmap from: " + url);
} finally {
closeStream(in);
closeStream(out);
}
return bitmap;
}
するとImageViewのにビットマップを設定するためにImageview.setImageBitmapを使用
書いたクラスを取り扱うことのようで経常に私の様々なプロジェクト:
https://github.com/koush/UrlImageViewHelper
UrlImageViewHelper記る ImageViewイメージで見られる 時りましたが、修正いたしました。
サンプルはGoogleのイメージ 検索とロード/ショーの結果 非同期.
UrlImageViewHelper自動的に ダウンロード、保存、キャッシュのすべての 画像のurlをBitmapDrawables.重複したurlないことに積み込み メモリ。ビットマップメモリ管理 による弱参照のハッシュテーブル などの画像がなくなっ 使用される、ゴミ 自動測定します。
受け入れ答えは、上記のそれは1秒か2秒のためのUIをフリーズ素晴らしいです。周り見て私は、単純なasynctaskは、この問題を解消することを発見しました。
あなたの活動の終わりに、このクラスを追加asynctaskを使用するには:
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
そして使用してのonCreate()メソッドから呼び出します:
new DownloadImageTask((ImageView) findViewById(R.id.imageView1))
.execute(MY_URL_STRING);
これは、ユーザのネットワークの速度に応じて分割された第二後現れ素早くロードされた活性およびImageViewのである。
また、URLから画像をロードするために、このLoadingImageViewビューを使用することができます:
http://blog.blundellapps.com/imageview-with-loading-スピナー/ の
あなたは、URLのイメージビューをインスタンス化することができますそのリンクからクラスファイルを追加した後:
XMLでます:
<com.blundell.tut.LoaderImageView
android:id="@+id/loaderImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
image="http://developer.android.com/images/dialog_buttons.png"
/>
コードで
final LoaderImageView image = new LoaderImageView(this, "http://developer.android.com/images/dialog_buttons.png");
そして使用して、それを更新します:
image.setImageDrawable("http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png");
public class LoadWebImg extends Activity {
String image_URL=
"http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView bmImage = (ImageView)findViewById(R.id.image);
BitmapFactory.Options bmOptions;
bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 1;
Bitmap bm = LoadImage(image_URL, bmOptions);
bmImage.setImageBitmap(bm);
}
private Bitmap LoadImage(String URL, BitmapFactory.Options options)
{
Bitmap bitmap = null;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in, null, options);
in.close();
} catch (IOException e1) {
}
return bitmap;
}
private InputStream OpenHttpConnection(String strURL) throws IOException{
InputStream inputStream = null;
URL url = new URL(strURL);
URLConnection conn = url.openConnection();
try{
HttpURLConnection httpConn = (HttpURLConnection)conn;
httpConn.setRequestMethod("GET");
httpConn.connect();
if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
inputStream = httpConn.getInputStream();
}
}
catch (Exception ex)
{
}
return inputStream;
}
}
こんにちは、私はほとんどの最も簡単なコードはこれを試してみる。
public class ImageFromUrlExample extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView imgView =(ImageView)findViewById(R.id.ImageView01);
Drawable drawable = LoadImageFromWebOperations("http://www.androidpeople.com/wp-content/uploads/2010/03/android.png");
imgView.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) {
System.out.println("Exc="+e);
return null;
}
}
}
main.xml
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:id="@+id/ImageView01"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
これを試します
私が最近発見したスレッド<のhref =「https://stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-images-in-listview」 >ここを、私は画像とリストビューのために同じようなことをしなければならない、しかし、あなたは(jleedevで)が示され、最初のサンプルクラスで読むことができるよう原理は、単純であるとして。 あなたは、
(ウェブから)画像の入力ストリームを取得しますprivate InputStream fetch(String urlString) throws MalformedURLException, IOException {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet request = new HttpGet(urlString);
HttpResponse response = httpClient.execute(request);
return response.getEntity().getContent();
}
そして、あなたはDrawableのように画像を保存します(setImageDrawable経由)ImageViewのにそれを渡すことができます。ここでも、上のコードスニペットからスレッド全体を見てみましょう。
InputStream is = fetch(urlString);
Drawable drawable = Drawable.createFromStream(is, "src");
たくさん良い情報はこちらの---最近、私は、このクラスと呼ばれSmartImageViewいうものです。非常に簡単に取り入れて使用します。
http://loopj.com/android-smart-image-view/
https://github.com/loopj/android-smart-image-view
更新:その結書 このブログ, ので、チェックアウトを使用SmartImageView.
第2回更新:でもピカソのこと(上記参照)をお勧めします。:)
私の意見のために、このようなタスクのための最高の近代的なライブラリは、広場でピカソのです。これは、ワンライナーでURLでImageViewのに画像をロードすることができます:
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
このはAsyncTask
上で示唆したように返事が遅れは、意志と私は、この問題のためにもう一つの方法を発見したビットをグーグル後になりますです。
Drawable drawable = Drawable.createFromStream((InputStream) new URL("url").getContent(), "src");
imageView.setImageDrawable(drawable);
ここでは、完全な機能があります:
public void loadMapPreview () {
//start a background thread for networking
new Thread(new Runnable() {
public void run(){
try {
//download the drawable
final Drawable drawable = Drawable.createFromStream((InputStream) new URL("url").getContent(), "src");
//edit the view in the UI thread
imageView.post(new Runnable() {
public void run() {
imageView.setImageDrawable(drawable);
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
インターネットにアクセスするには、あなたのAndroidManifest.xml
に以下の権限を追加することを忘れないでください。
<uses-permission android:name="android.permission.INTERNET" />
私は自分自身と私はまだ問題に直面していないこれを試しています。
imageView.setImageBitmap(BitmapFactory.decodeStream(imageUrl.openStream()));//try/catch IOException and MalformedURLException outside
これはあなたを助ける...
それにImageViewのとロードイメージを定義します.....
Imageview i = (ImageView) vv.findViewById(R.id.img_country);
i.setImageBitmap(DownloadFullFromUrl(url));
次に、このメソッドを定義します:
public Bitmap DownloadFullFromUrl(String imageFullURL) {
Bitmap bm = null;
try {
URL url = new URL(imageFullURL);
URLConnection ucon = url.openConnection();
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
bm = BitmapFactory.decodeByteArray(baf.toByteArray(), 0,
baf.toByteArray().length);
} catch (IOException e) {
Log.d("ImageManager", "Error: " + e);
}
return bm;
}
private Bitmap getImageBitmap(String url) {
Bitmap bm = null;
try {
URL aURL = new URL(url);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
} catch (IOException e) {
Log.e(TAG, "Error getting bitmap", e);
}
return bm;
}
これを行うには、シンプルでクリーンな方法は、総理のオープンソースのライブラリを使用することです。
String img_url= //url of the image
URL url=new URL(img_url);
Bitmap bmp;
bmp=BitmapFactory.decodeStream(url.openConnection().getInputStream());
ImageView iv=(ImageView)findviewById(R.id.imageview);
iv.setImageBitmap(bmp);
このコードはテストされ、それが完全に働いている。
URL req = new URL(
"http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png"
);
Bitmap mIcon_val = BitmapFactory.decodeStream(req.openConnection()
.getInputStream());
のリストビュー・グリッド・ビューのように、任意の容器でImageViewのために働く、通常のレイアウトの
private class LoadImagefromUrl extends AsyncTask< Object, Void, Bitmap > {
ImageView ivPreview = null;
@Override
protected Bitmap doInBackground( Object... params ) {
this.ivPreview = (ImageView) params[0];
String url = (String) params[1];
System.out.println(url);
return loadBitmap( url );
}
@Override
protected void onPostExecute( Bitmap result ) {
super.onPostExecute( result );
ivPreview.setImageBitmap( result );
}
}
public Bitmap loadBitmap( String url ) {
URL newurl = null;
Bitmap bitmap = null;
try {
newurl = new URL( url );
bitmap = BitmapFactory.decodeStream( newurl.openConnection( ).getInputStream( ) );
} catch ( MalformedURLException e ) {
e.printStackTrace( );
} catch ( IOException e ) {
e.printStackTrace( );
}
return bitmap;
}
/** Usage **/
new LoadImagefromUrl( ).execute( imageView, url );
例外処理と非同期タスクとバージョン:
AsyncTask<URL, Void, Boolean> asyncTask = new AsyncTask<URL, Void, Boolean>() {
public Bitmap mIcon_val;
public IOException error;
@Override
protected Boolean doInBackground(URL... params) {
try {
mIcon_val = BitmapFactory.decodeStream(params[0].openConnection().getInputStream());
} catch (IOException e) {
this.error = e;
return false;
}
return true;
}
@Override
protected void onPostExecute(Boolean success) {
super.onPostExecute(success);
if (success) {
image.setImageBitmap(mIcon_val);
} else {
image.setImageBitmap(defaultImage);
}
}
};
try {
URL url = new URL(url);
asyncTask.execute(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
これをやってみましょう、希望すことが大事だと申し上げました解決問題です。
こちらについて説明しの使いかた"AndroidQuery"外部の図書館のための負荷からの画像のurl/サーバーをasyncTaskるとともにキャッシュにロード画像をデバイスファイルまたはキャッシュです。
- ダウンロード"AndroidQuery"図書館 こちらから
- コピー/貼り付けこのjarへのプロジェクトlibフォルダとフォルダに追加します図書館プロジェクト-ビルド-パス
- 現在で示デモをどのように使用します。
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imageFromUrl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"/>
<ProgressBar
android:id="@+id/pbrLoadImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</FrameLayout>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
private AQuery aQuery;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
aQuery = new AQuery(this);
aQuery.id(R.id.imageFromUrl).progress(R.id.pbrLoadImage).image("http://itechthereforeiam.com/wp-content/uploads/2013/11/android-gone-packing.jpg",true,true);
}
}
Note : Here I just implemented common method to load image from url/server but you can use various types of method which can be provided by "AndroidQuery"to load your image easily.