2015-08-31 131 views
1

我试图将以下图像链接加载到imageview。 https://pbs.twimg.com/profile_banners/169252109/1422362966 图像大小加载imageview不是问题吗?如何在不下载图片的情况下将图像加载到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());

什么是我错了工作?

+0

你的代码是什么意思? – greenapps

+0

我的意思是应用程序停止在该行工作:) – Trinity

回答

4

你为什么不试试Picasso

它是如此简单:

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

而且,你还可以将位图,运用色彩,调整,以防止内存问题,所有的东西。

+0

非常感谢你的人! – Trinity

相关问题