关于android:URL变成一个ImageView | 珊瑚贝

URL into a ImageView


我查看了整个 stackoverflow 并没有找到一个解决方案!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
    package com.example.sfgsfg;

import java.io.InputStream;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.ImageView;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageView img = (ImageView) findViewById(R.id.imageView1);
        try {
            URL url = new URL(
                   “http://0.tqn.com/d/webclipart/1/0/5/l/4/floral-icon-5.jpg”);
            // try this url =
            //”http://0.tqn.com/d/webclipart/1/0/5/l/4/floral-icon-5.jpg”
            HttpGet httpRequest = null;

            httpRequest = new HttpGet(url.toURI());

            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = (HttpResponse) httpclient
                    .execute(httpRequest);

            HttpEntity entity = response.getEntity();
            BufferedHttpEntity b_entity = new BufferedHttpEntity(entity);
            InputStream input = b_entity.getContent();

            Bitmap bitmap = BitmapFactory.decodeStream(input);

            img.setImageBitmap(bitmap);

        } catch (Exception ex) {

        }

    }

}

这是我最近的尝试,我已经尝试了以下所有方法:
在 ImageView 中显示图像(来自 Url)
Android,在等于 ImageView\\ 的图像的 URL 处制作图像
如何在 Android 中通过 URL 加载 ImageView?
如何从 url 为 imageView 设置图像
Android 应用显示来自 URL

的图像

没有任何作用!而且我需要它而不使用 AsyncTask!请帮忙!

  • 没有 asynctask 你会得到 anr 错误
  • @evgeny Goldin 您想下载图像并希望在图像视图中显示还是想要延迟加载?只是为了澄清。
  • 您可能会在主线程异常上获得网络。您不能使用主线程执行网络任务,使用 AsyntTask 或 Thread 从网络加载图像,然后在主线程上调用 setImageBitmap。但是,除非这只是一个学习练习,否则您可能应该使用许多出色的库来为您完成所有这些工作 *检查 Jorge Alfaro 的答案)
  • 我需要以某种方式从 URL 获取位图,所以稍后我可以在 ImageView 上设置它。我不介意它是否使用线程,但不是异步任务!而且我需要它而不使用任何外部罐子..
  • URL newurl = 新 URL(photo_url_str); mIcon_val = BitmapFactory.decodeStream(newurl.openConnection() .getInputStream()); profile_photo.setImageBitmap(mIcon_val);这段代码看起来很简单。为什么它不起作用?


您可以使用以下链接之一:

http://androidexample.com/Download_Images_From_Web_And_Lazy_Load_In_ListView_-_Android_Example/index.php?view=article_discription


我强烈推荐使用这个库

  • 我需要以某种方式从 URL 获取位图,所以稍后我可以在 ImageView 上设置它。我不介意它是否使用线程,但不是异步任务!而且我需要它而不使用任何外部罐子..


n


来源:https://www.codenong.com/22767684/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9110.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?