Yusof Yusof - 3 years ago 137
Android Question

Unable to load profile image and header background from URL

I followed this tutorial step by step but when I want to load profile image and header background nothing displayed. I added internet permission and wifi is on and initialized drawerimageloader. What could the problem be?

Main activity:

import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;

import com.mikepenz.materialdrawer.DrawerBuilder;
import com.mikepenz.materialdrawer.AccountHeader;
import com.mikepenz.materialdrawer.AccountHeaderBuilder;
import com.mikepenz.materialdrawer.model.ProfileDrawerItem;
import com.mikepenz.materialdrawer.model.interfaces.IProfile;

import com.bumptech.glide.Glide;


public class MainActivity extends AppCompatActivity {;

Toolbar toolbar;

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

toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

new DrawerBuilder().withActivity(this).build();

AccountHeader headerResult = new AccountHeaderBuilder()
.withActivity(this)
.addProfiles(
new ProfileDrawerItem().withName("Mike
Penz").withEmail("mikepenz@gmail.com").withIcon(Uri.
parse("https://avatars3.githubusercontent.com/u/887462?v=3&s=460"))
)
.withOnAccountHeaderListener(new
AccountHeader.OnAccountHeaderListener() {
@Override
public boolean onProfileChanged(View view, IProfile profile,
boolean currentProfile) {
return false;
}
})
.build();

new DrawerBuilder()
.withActivity(this)
.withAccountHeader(headerResult)
.build();

ImageView view = headerResult.getHeaderBackgroundView();
Glide.with(this).load("www.api.androidhive.info/images/nav-menu-header-
bg.jpg").into(view);
}

}


custom activity:

import android.app.Application;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.mikepenz.iconics.IconicsDrawable;
import com.mikepenz.materialdrawer.util.AbstractDrawerImageLoader;
import com.mikepenz.materialdrawer.util.DrawerImageLoader;
import com.mikepenz.materialdrawer.util.DrawerUIUtils;

/**
* Created by mikepenz on 27.03.15.
*
*/
public class CustomApplication extends Application {

@Override
public void onCreate() {
super.onCreate();

/*
//initialize and create the image loader logic
DrawerImageLoader.init(new AbstractDrawerImageLoader() {
@Override
public void set(ImageView imageView, Uri uri, Drawable placeholder)
{

Picasso.with(imageView.getContext()).load(uri).placeholder(placeholder)
.into(imageView);
}
@Override
public void cancel(ImageView imageView) {
Picasso.with(imageView.getContext()).cancelRequest(imageView);
}
});
*/

//initialize and create the image loader logic
DrawerImageLoader.init(new AbstractDrawerImageLoader() {
@Override
public void set(ImageView imageView, Uri uri, Drawable placeholder,
String tag) {
Glide.with(imageView.getContext()).load(uri)
.placeholder(placeholder).into(imageView);
}

@Override
public void cancel(ImageView imageView) {
Glide.clear(imageView);
}

@Override
public Drawable placeholder(Context ctx, String tag) {
//define different placeholders for different imageView targets
//default tags are accessible via the DrawerImageLoader.Tags
if (DrawerImageLoader.Tags.PROFILE.name().equals(tag)) {
return DrawerUIUtils.getPlaceHolder(ctx);
} else if
(DrawerImageLoader.Tags.ACCOUNT_HEADER.name().equals(tag)) {
return new IconicsDrawable(ctx).iconText("
").backgroundColorRes(com.mikepenz.materialdrawer.R.color.primary)
.sizeDp(56);
} else if ("customUrlItem".equals(tag)) {
return new IconicsDrawable(ctx).iconText("
").backgroundColorRes(R.color.md_red_500).sizeDp(56);
}

//we use the default one for
//DrawerImageLoader.Tags.PROFILE_DRAWER_ITEM.name()

return super.placeholder(ctx, tag);
}
});
}
}


manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ir.techend.yusof.ghaza">

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

Answer Source

Please remove the duplicated

new DrawerBuilder().withActivity(this).build();

Which you have in your MainActivity, You must only .build() one Drawer (and in some cases .append() another one)

The code of the Drawer will make sure not 2 DrawerLayout's are inflated.

In addition I checked the url for the header image. This one does not exist. (http://www.api.androidhive.info/images/nav-menu-header-bg.jpg returns a 404)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download