android - 如何在android中的imageview中从url网站获取图像

标签 android android-layout imageview android-imageview

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

这是我的主要 Activity

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>

最佳答案

在主线程中进行网络 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做得更好 比我还好,包括透明的本地缓存、多加载线程管理和 启用有效的加载前调整大小策略。除了咖啡 :)

关于android - 如何在android中的imageview中从url网站获取图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24535924/

相关文章:

android - 如何放置一个 android 组件偏离中心?

android - 从我自己的应用程序读取另一个应用程序的 .apk

android - 为什么我在使用 retrofit 的 android 上没有网络连接时收到 503 状态代码?

android - 打开 .xml 文件后 Eclipse 崩溃

android - 如何实现 PIN 码屏幕

android - 将列表项的高度设置为两行的高度

android - 自定义 ListView,其中所有图像都具有相同的大小

android - 是否可以在android中为下载的图像生成资源ID

java - 本地化不显示在设备上

android - Android Activity 可以在 finish() 之后恢复吗?