因此,在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()
謝謝大家的時間和精力!
convertUrlToBitmap是一個單獨的類或只是它的裏面你Activity ..? –
在我的活動內 – Alphonse