簡體   English   中英

Android SDK,通過URL設置ImageView位圖

[英]Android SDK, Set ImageView bitmap from URL

如何將圖像從URL放入ImageView

我不想使用任何庫,我更喜歡使用普通的舊方法。 不要判斷我。 就像我說的那樣,在我開始工作之前,但奇怪的是它不再

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ImageView damnit = (ImageView)findViewById(R.id.imageview);
            try {
                URL url = new URL("https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png");

                InputStream in = url.openStream();
                Bitmap bitt = BitmapFactory.decodeStream(in);
                damnit.setImageBitmap(bitt);
            } catch (Exception e) {
                e.printStackTrace();
            }
}

編輯!

我真是個白痴,很抱歉讓您花費時間……我必須有一個runOnUI線程,並且里面有“ damnit.setImageBitmap(bitt);”。

您正在嘗試從MainThread獲取URL。 這是不可能的。 這是您將得到的錯誤: android.os.NetworkOnMainThreadException

例如,嘗試使用AsyncTask從URL檢索圖像。

@Override
public void onCreate() {
    super.onCreate();

    ImageView damnit = (ImageView)findViewById(R.id.imageview);
    DownloadTask downloadTask = new DownloadTask(damnit);
    downloadTask.execute();
}

class DownloadTask extends AsyncTask <Void, Void, Bitmap>{
    private ImageView imageView;

    public DownloadTask(ImageView imageView) {
        this.imageView = imageView;
    }
    @Override
    protected Bitmap doInBackground(Void... params) {
        try {
            URL url = new URL("https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png");
            InputStream in = url.openStream();
            return BitmapFactory.decodeStream(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        super.onPostExecute(bitmap);
        if (bitmap != null) {
            imageView.setImageBitmap(bitmap);
        }
    }
}

或使用一個庫,例如Glide

如何設置滑行:

build.gradle:

repositories {
  mavenCentral() // jcenter() works as well because it pulls from Maven Central
}

dependencies {
  compile 'com.github.bumptech.glide:glide:3.7.0'
  compile 'com.android.support:support-v4:19.1.0'
}

的onCreate:

Glide.with(this)
    .load("https://www.some-url.com/image.jpg")
    .into(imageView);

使用Glide庫加載圖像並設置為ImageView:

為什么我應該推薦Glide庫?

因為它非常快並且消耗更少的內存

Glide是適用於Android的快速高效的開源媒體管理和圖像加載框架,它將媒體解碼,內存和磁盤緩存以及資源池包裝到一個簡單易用的界面中。

如何設置滑行:

將Glide庫設置為項目的不同方法

1.您可以從GitHub的發行頁面下載一個jar。

要么

2.使用Gradle-> build.gradle(Module:app)

repositories {
  mavenCentral() // jcenter() works as well because it pulls from Maven Central
}

dependencies {
  compile 'com.github.bumptech.glide:glide:3.7.0'
  compile 'com.android.support:support-v4:19.1.0'
}

要么

3,使用Maven

<dependency>
  <groupId>com.github.bumptech.glide</groupId>
  <artifactId>glide</artifactId>
  <version>3.7.0</version>
</dependency>
<dependency>
  <groupId>com.google.android</groupId>
  <artifactId>support-v4</artifactId>
  <version>r7</version>
</dependency>

您如何使用Glide?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ImageView damnit = (ImageView)findViewById(R.id.imageview);

    Glide.with(this).load("https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png").into(damnit);

    }

快樂編碼:)

您可以使用圖片庫輕松完成此操作。 嘗試使用畢加索(Picasso),這是一個用於從Web下載和緩存圖像的開源庫,它也易於使用。

只需在gradle文件的“ dependencies”部分中添加以下行:

compile 'com.squareup.picasso:picasso:2.5.2'

然后,開始像這樣使用它:

Picasso.with(context)
    .load(url)
    .placeholder(R.drawable.user_placeholder)
    .error(R.drawable.user_placeholder_error)
    .into(imageView);

重要說明 :避免將空url傳遞給它,因為它將引發異常。

您可以使用UrlImageViewHelper庫。 在此處獲取它-http : //repository.sonatype.org/service/local/artifact/maven/redirect?r=central-proxy&g=com.koushikdutta.urlimageviewhelper&a=urlimageviewhelper&v=LATEST

並建立一種方法並在您想要的任何地方使用它-

public void setPicture(Context context, String url, ImageView imageView, int placeholder) {
    UrlImageViewHelper.setUrlDrawable(imageView, url, placeholder);
}

我認為您應該使用Universal Image Loader。 這是一個很棒的圖書館。 訪問: https//github.com/nostra13/Android-Universal-Image-Loader

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM