【问题标题】:How to get image from url website in imageview in android如何在android的imageview中从url网站获取图像
【发布时间】:2014-08-23 12:40:20
【问题描述】:

我正在尝试从 url 网站获取 ImageView 中的图像,但图像未显示, 这段代码有什么问题? 这是image 的网址。

这是我的主要活动

ImageView i;
private Bitmap bitmap;

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

    i=(ImageView)findViewById(R.id.ImageView1);




    bitmap=GetBitmapfromUrl("http://test-dashboard1.seeloz.com/system/images/products_images/86/5454544114_1401886223?1401886223");
    i.setImageBitmap(bitmap);


}


public Bitmap GetBitmapfromUrl(String scr) {
    try {
        URL url=new URL(scr);
        HttpURLConnection connection=(HttpURLConnection)url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input=connection.getInputStream();
        Bitmap bmp = BitmapFactory.decodeStream(input);
        return bmp;



    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;

    }
}}

在 XML 文件中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="85dp"
        android:layout_marginTop="179dp"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

我的 AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.image"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.image.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

【问题讨论】:

  • GetBitmapfromUrl 函数的结果是什么?
  • 您应该发布带​​有错误和堆栈跟踪的 logcat。您可能会有一个 NetworkOnMainThreadException。您必须将代码放在 AsyncTask 或线程中。此外,清单中的互联网许可。
  • 你能澄清一下这个问题吗?
  • 另外,我无法从您提供的网址中看到图片。网址是否正确。

标签: android android-layout imageview android-imageview


【解决方案1】:

在主线程中做网络 IO 是邪恶的。最好避免。

另外 - 你的 url 资源访问是错误的。

改用这样的东西:

 private Bitmap bmp;

   new AsyncTask<Void, Void, Void>() {                  
        @Override
        protected Void doInBackground(Void... params) {
            try {
                InputStream in = new URL(IMAGE_URL).openStream();
                bmp = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
               // log error
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            if (bmp != null)
                imageView.setImageBitmap(bmp);
        }

   }.execute();

这是将 url 资源加载到显示中的“旧方式”。坦率地说,我已经很久 没有写过这样的代码了。 VolleyPicasso 做得更好 比我,包括透明的本地缓存、多个加载器线程管理和 启用有效的 resize-before-load 策略。除了咖啡:)

【讨论】:

    【解决方案2】:

    试试Picasso。它应该花费您几乎一整行代码并且不到一分钟的时间来设置它。

    在你的情况下,那将是:

    Picasso.with(context).load(""http://imageurlgoeshere").into(i);
    

    【讨论】:

      【解决方案3】:

      如果您要从应用中的 URL 加载多张图片,那么绝对值得研究:

      nostra13's "Universal Image Loader"

      这是一个很棒的库,具有大量功能,可以显示来自 URL 的图像、转换为位图等。

      一旦你包含了类并声明了 imageloader + imageloader 配置,它就很简单:

      imageLoader.displayImage("http://www.yoursite.com/my_picture.png", imageView);
      

      其中 imageView 是您希望图像出现在其中的 imageView。

      【讨论】:

        【解决方案4】:

        您的问题不在于代码,而在于服务器。试试这个示例网址,告诉我它是否有效:

        http://cdn.sstatic.net/stackoverflow/img/apple-touch-icon.png

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-02-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多