无法从URL加载配置文件图像和标题背景

时间:2017-07-25 20:09:25

标签: android imageview navigation-drawer materialdrawer

我一步一步跟着this tutorial,但是当我想加载个人资料图片和标题背景时,没有显示任何内容。我添加了互联网许可,并启用了wifi并初始化了drawerimageloader。问题是什么?

主要活动:

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

  }

自定义活动:

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>

2 个答案:

答案 0 :(得分:1)

请删除重复的

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

您在MainActivity中所拥有的内容,您必须.build()一个Drawer(在某些情况下.append()另一个{}}

Drawer的代码将确保2 DrawerLayout}不会被夸大。

另外,我检查了网址图片的网址。这个不存在。 (http://www.api.androidhive.info/images/nav-menu-header-bg.jpg返回404)

答案 1 :(得分:0)

使用Picasso库

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

并且在你的gradle依赖项中

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