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

How to get image from url website in imageview in android(如何在android的imageview中从url网站获取图像)
本文介绍了如何在android的imageview中从url网站获取图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to get image in ImageView from url website but the image not show so, What is the wrong in this code? This is the url of the 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

my 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是邪恶的.最好避免.

Doing network IO in the main thread is evil. Better to avoid.

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

Also - your url resource access is wrong.

改用这样的东西:

 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 资源加载到显示中的旧方式".坦率地说,我已经很久 没有写过这样的代码了.Volley 和 毕加索 做得更好比我,包括透明的本地缓存、多个加载器线程管理和启用有效的 resize-before-load 策略.除了咖啡:)

This is the 'old way' of loading url resources into display. Frankly speaking, I have not written such code in a long time. Volley and Picasso simply do it much better than me, including transparent local cache, multiple loader-threads management and enabling effective resize-before-load policies. All but coffee :)

这篇关于如何在android的imageview中从url网站获取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)