如何使用url将图像加载到imageview而不下载图像

时间:2015-08-31 20:11:29

标签: android

我正在尝试将以下图片链接加载到imageview。 https://pbs.twimg.com/profile_banners/169252109/1422362966 加载imageview可能会导致图像尺寸出现问题吗?

activity_profile.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/container_toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <include
                    android:id="@+id/toolbar"
                    layout="@layout/toolbar" />
            </LinearLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="140dp"
                android:layout_alignParentTop="true"
                android:background="@color/background_material_dark">

                <ImageView
                    android:id="@id/profile_banner"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_centerInParent="true"
                    android:scaleType="centerCrop"
                    />

                <ImageView
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    android:scaleType="fitCenter"
                    android:layout_centerInParent="true" />

            </RelativeLayout>

        </LinearLayout>

        <fragment
            android:id="@+id/fragment_navigation_drawer"
            android:layout_width="@dimen/nav_drawer_width"
            android:name="activity.FragmentDrawer"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:layout="@layout/fragment_navigation_drawer"
            tools:layout="@layout/fragment_navigation_drawer" />

    </android.support.v4.widget.DrawerLayout

ActivityProfile.java

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

        mToolbar = (Toolbar) findViewById(R.id.toolbar);

        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        drawerFragment = (FragmentDrawer)
                getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
        drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar);
        drawerFragment.setDrawerListener(this);

        final ImageView imgView =(ImageView)findViewById(R.id.profile_banner);

        TwitterSession session = Twitter.getSessionManager().getActiveSession();

        final long userID = session.getUserId();

        MyTwitterApiClients apiclients = new MyTwitterApiClients(session);

        final Bitmap bmp = null;

        apiclients.getProfileBannerService().show(userID, null, new Callback<User>() {
            @Override
            public void success(Result<User> result) {

                String imageUrl = result.data.profileBannerUrl;
                URL url = null;
                Bitmap bmp = null;
                try {
                    url = new URL(imageUrl);
                    bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());

                    imgView.setImageBitmap(bmp);

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

应用停止在此行bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());

工作

我错了什么?

1 个答案:

答案 0 :(得分:4)

为什么不尝试Picasso

这很简单:

Picasso
    .with(context)
    .load("https://pbs.twimg.com/profile_banners/169252109/1422362966")
    .into(imgView);

而且,您还可以转换位图,应用色调,调整大小以防止内存问题,所有这些。