2017-08-15 7 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は別のクラスまたはちょうどそのあなたの活動の中にありますか..? –

+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を使用してダウンロードする代わりに、を使用できますlibに、また、あなたがこのように使用することができグライドを使用Glide

を使用することをお勧め公式Androidのドキュメント、

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

あなたがonBitmapLoaded方法でインターフェースコールバックを作成し、あなたのPARAMとしてこのインタフェースを使用することができますAsyncTask。

しかし、画像の読み込みのためにあなたが正確に非常によく文書化され、使いやすい、非同期画像フェッチのために設計されているピカソやグライドのようなサードパーティ製のlibraryiesを使用することができることを知っています。それはUIスレッドで実行されるようasynctaskのpostExecute方法のいずれかのビューを設定し

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

https://github.com/bumptech/glide

0

。ポスト実行メソッドで既にビットマップを返すので、この変更を行うだけです。これはあなたのasynctaskがあなたの活動にあることを前提としています。すでに画像のURLを持っているので

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) 

} 

}

もう一つの非常に簡単かつ効率的なソリューションは、ピカソやグライドライブラリを使用しています。 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ライブラリを使用することを強くお勧めします。

関連する問題