したがって、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は別のクラスまたはちょうどそのあなたの活動の中にありますか..? –
私の活動の中に – Alphonse