相关疑难解决方法(0)

519
推荐指数
15
解决办法
52万
查看次数

将url图像设置为图像视图

可能重复:
是否可以使用BitmapFactory.decodeFile方法解码来自http位置的图像?

我在设置网址图像到图像视图时遇到问题.我试过以下方法

方法1:

Bitmap bimage=  getBitmapFromURL(bannerpath);
image.setImageBitmap(bimage);

 public static Bitmap getBitmapFromURL(String src) {
        try {
            Log.e("src",src);
            URL url = new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            Log.e("Bitmap","returned");
            return myBitmap;
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("Exception",e.getMessage());
            return null;
        }
    }
Run Code Online (Sandbox Code Playgroud)

方法2:

    Drawable drawable = LoadImageFromWebOperations(bannerpath);
    image.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 …
Run Code Online (Sandbox Code Playgroud)

url android imageview

13
推荐指数
1
解决办法
4万
查看次数

在Android上下载imageview的图像

我已经看到了这个问题:android如何下载1mb图像文件并设置为ImageView
它没有解决我的问题,因为它只显示了你已经拥有它如何显示位图.

我正在尝试从URL下载图像,以便在Android设备上显示ImageView.我不知道该怎么做.

我在互联网上看了一下,这是我到目前为止的代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    //Set local image
    ImageView image = (ImageView) findViewById(R.id.test_image);
    image.setImageResource(R.drawable.test2);

    //Prepare to download image
    URL url;        
    InputStream in;

    //BufferedInputStream buf;
    try {
        url = new URL("http://i.imgur.com/CQzlM.jpg");
        in = url.openStream();

        out = new BufferedOutputStream(new FileOutputStream("testImage.jpg"));
        int i;

         while ((i = in.read()) != -1) {
             out.write(i);
         }
         out.close();
         in.close();

        buf = new BufferedInputStream(in);
        Bitmap bMap = BitmapFactory.decodeStream(buf);
        image.setImageBitmap(bMap);
        if (in != null) {
        in.close();
        }
        if (buf != null) …
Run Code Online (Sandbox Code Playgroud)

android download android-imageview

5
推荐指数
1
解决办法
2万
查看次数

标签 统计

android ×3

imageview ×2

android-imageview ×1

bitmap ×1

download ×1

url ×1