2017-08-15 55 views
0

因此,在onCreate中,我嘗試使用另一個導航視圖標題更改圖像,我可以通過url訪問該標題。我使用的是的AsyncTask做到這一點:使用AsyncTask更改菜單標題圖像

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

     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
     navigationView.setNavigationItemSelectedListener(this); 

     View headerView = navigationView.getHeaderView(0); 

     User u = MainProvider.sharedInstance().getCurrentUser(this); 
     TextView usernameText = (TextView) headerView.findViewById(R.id.usernameText); 

     ImageView profilePicture = (ImageView)headerView.findViewById(R.id.profilePicture); 
     String profilePictureUrl = u.getSettings().get("profile_picture").getAsString(); 
     new convertUrlToBitmap().execute(profilePictureUrl); 
     profilePicture.setImageBitmap(); 
    } 

而且我的AsyncTask是這樣的:

class convertUrlToBitmap extends AsyncTask<String, Void, Bitmap> { 

    private Exception exception; 

    protected Bitmap doInBackground(String... urls) { 
     try { 
      URL url = new URL(urls[0]); 
      HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
      connection.setDoInput(true); 
      connection.connect(); 
      InputStream input = connection.getInputStream(); 
      Bitmap myBitmap = BitmapFactory.decodeStream(input); 
      connection.disconnect(); 
      return myBitmap; 
     } catch (IOException e) { 
      // Log exception 
      return null; 
     } 
    } 

    protected void onPostExecute(Bitmap myBitmap) { 
     // TODO: check this.exception 
     // TODO: do something with the feed 

    } 
} 

我不明白的是如何使用我從我的setImageBitmap方法任務獲得:

profilePicture.setImageBitmap() 

謝謝大家的時間和精力!

+0

convertUrlToBitmap是一個單獨的類或只是它的裏面你Activity ..? –

+0

在我的活動內 – Alphonse

回答

0

而不是增加profilePicture局部變量將其添加爲全局變量,並在onPostExecute,剛剛設置的位圖

您的主要活動

private ImageView profilePicture 

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

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
    navigationView.setNavigationItemSelectedListener(this); 

    View headerView = navigationView.getHeaderView(0); 

    User u = MainProvider.sharedInstance().getCurrentUser(this); 
    TextView usernameText = (TextView) headerView.findViewById(R.id.usernameText); 

    profilePicture = (ImageView)headerView.findViewById(R.id.profilePicture); 
    String profilePictureUrl = u.getSettings().get("profile_picture").getAsString(); 
    new convertUrlToBitmap().execute(profilePictureUrl); 
    profilePicture.setImageBitmap(); 
} 

您對PostExecute

protected void onPostExecute(Bitmap myBitmap) { 
    // TODO: check this.exception 
    // TODO: do something with the feed 
    profilePicture.setImageBitmap(myBitmap) 
} 

注意: 而不是使用HttpURLConnection下載,您可以使用Glide lib下,Android官方文檔也建議使用Glide

利用Glide,你可以使用這樣,

Glide.with(this).load(profilePictureUrl).diskCacheStrategy(DiskCacheStrategy.ALL).placeholder(R.drawable.ic_user).into(profilePicture); 
0

您可以創建一個接口回調與方法onBitmapLoaded,並使用這個接口的PARAM你的AsyncTask。

但是,要知道,對於圖像加載,您可以使用像Picasso或Glide這樣的第三方庫,它們非常適合異步圖像抓取,非常詳細的文檔記錄和易於使用。

http://square.github.io/picasso/

https://github.com/bumptech/glide

0

,因爲它在UI線程運行在設定的的AsyncTask方法postExecute任何意見。你已經在post執行方法中返回位圖,所以你只需要做這個改變。這是提供你的asynctask在你的活動。

class convertUrlToBitmap extends AsyncTask<String, Void, Bitmap> { 

private Exception exception; 

protected Bitmap doInBackground(String... urls) { 
    try { 
     URL url = new URL(urls[0]); 
     HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
     connection.setDoInput(true); 
     connection.connect(); 
     InputStream input = connection.getInputStream(); 
     Bitmap myBitmap = BitmapFactory.decodeStream(input); 
     connection.disconnect(); 
     return myBitmap; 
    } catch (IOException e) { 
     // Log exception 
     return null; 
    } 
} 

protected void onPostExecute(Bitmap myBitmap) { 
    // TODO: check this.exception 
    // TODO: do something with the feed 

     profilePicture.setImageBitmap(myBitmap) 

} 

}

另一個非常簡單而有效的解決方案是使用畢加索或滑翔庫既然你已經有了圖像的URL。它會減少你創造asynctask的工作。只需在你的Gradle中添加這個庫。

compile 'com.squareup.picasso:picasso:2.5.2' 

並在您的活動中進行此更改。

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

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
    navigationView.setNavigationItemSelectedListener(this); 

    View headerView = navigationView.getHeaderView(0); 

    User u = MainProvider.sharedInstance().getCurrentUser(this); 
    TextView usernameText = (TextView) headerView.findViewById(R.id.usernameText); 

    ImageView profilePicture = (ImageView)headerView.findViewById(R.id.profilePicture); 
    String profilePictureUrl = u.getSettings().get("profile_picture").getAsString(); 
    Picasso.with(this) 
     .load(profilePictureUrl) 
     .placeHolder(defaultPicId) //this image will display until your result image is ready 
     .error(defaultErrorPicId) //this image will display in case of error. 
     .into(profilePicture) 
} 

我強烈建議在您的情況下使用picasso庫。