【问题标题】:Load image from URL从 URL 加载图像
【发布时间】:2011-05-16 20:23:01
【问题描述】:

我已经搜索并尝试了几种不同的方法。我在这两个方面都不足。这是我目前的方法:

package com.dop.mobilevforum;

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

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.VideoView;

public class Vforum extends Activity
{
    private String imgPath = "http://mysite.com/mv/vfdemo1/slides/slide1.jpg";

    private ImageView slideHolder;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.vforum);

        slideHolder = (ImageView) findViewById(R.id.slideHolder);

        Drawable drawable = LoadImageFromWebOperations(imgPath);
        slideHolder.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)
        {
            Log.w("LoadImageFromWebOperations",e.toString());
            return null;
        }
    }
}

LoadImageFromWebOperations 方法正在返回一个位图,所以我知道该部分正在工作。我认为它失败的是slideHolder.setImageDrawable(drawable);。这是我的 XML:

<?xml version="1.0" encoding="UTF-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/mobile_vforum_bg">
    <ImageView 
        android:id="@+id/slideHolder"
        android:layout_width="320px" 
        android:layout_height="240px">
    </ImageView>
</FrameLayout>

有什么想法吗?我只是黑屏,没有错误。

【问题讨论】:

  • 我见过的几个操作系统项目只是使用 WebView 来显示图像。
  • 使用网页视图要容易得多!

标签: android uri imageview


【解决方案1】:

您最好的选择是使用 WebView,但如果您真的想这样做,请不要使用 setImageDrawable。你应该使用setImageBitmap。如果您的 LoadImageFromWebOperations 返回位图,那么您不应该更改任何内容,但您正在使用的功能应该可以顺利运行。

【讨论】:

  • 是的,WebView 是要走的路。那太容易了。另外,当我说位图时,我的意思是一般来说就像一个图像。我应该更清楚。它确实返回了一个 Drawable。
【解决方案2】:

您的代码有效,亲爱的朋友,您的网址无效http://mysite.com/mv/vfdemo1/slides/slide1.jpg。

您可以使用它,它 100% 有效,但您的代码也有效

URL url = new URL("http://variable3.com/files/images/email-sig.jpg");
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
slideHolder.setImageBitmap(bmp);

【讨论】:

  • 我的 URL 确实有效。我更改了它,因为它是我不希望人们看到的信息。无论如何,我尝试了您的代码,但无法使其正常工作。我最终使用了 WebView
猜你喜欢
  • 1970-01-01
  • 2012-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-08
  • 2015-07-25
  • 2011-12-18
相关资源
最近更新 更多