无法从 URL 加载个人资料图片和 header 背景

Unable to load profile image and header background from URL

我按照 this tutorial 一步一步来,但是当我想加载个人资料图片和 header 背景时,没有任何显示。我添加了互联网权限并且 wifi 已打开并初始化了 drawerimageloader。可能是什么问题?

主要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);
  }

  }

自定义 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);
        }
    });
}
}

清单:

<?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>

请删除重复的

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

您的 MainActivity 中有哪些,您只能 .build() 一个 Drawer(在某些情况下 .append() 另一个)

Drawer 的代码将确保没有 2 DrawerLayout 被夸大。

此外,我检查了 url 中的 header 图片。这个不存在。 (http://www.api.androidhive.info/images/nav-menu-header-bg.jpg returns 一个 404)

为此使用 Picasso 库

 Picasso.with(YourActivityName.this).load(url_img).into(imageView);

并在您的 gradle 依赖项中放置

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