从URL将图像加载到android studio中的导航抽屉

时间:2018-04-06 19:17:29

标签: android navigation-drawer

我正在尝试将图像加载到android studio中的导航抽屉中。但该应用程序一直在关闭。我无法弄清楚它为什么会发生。图片大小为697x967。 我的导航抽屉xml文件如下所示:

<ImageView
    android:id="@+id/profilePic"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="@string/nav_header_desc"
    android:paddingTop="@dimen/nav_header_vertical_spacing"/>

并且主页中的代码如下所示放在onCreate方法下。

    ImageView profile_pic=findViewById(R.id.profilePic);
    Bundle bundle=getIntent().getExtras();
    String profile_url="https://www.rajagiritech.ac.in/stud/Photo/"+ bundle.getString("username")+".jpg";
    try{
        URL url = new URL(profile_url);
        Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
        profile_pic.setImageBitmap(bmp);
    }catch(MalformedURLException ex){
        ex.printStackTrace();
    }catch(IOException ex){
        ex.printStackTrace();
    }

我有一个活动调用此活动并传递用户名。

public void onClick(View v){
    EditText uid_form=findViewById(R.id.uid_form);
    String uname=uid_form.getText().toString();
    Intent intent=new Intent(getApplicationContext(),HomePage.class);
    intent.putExtra("username",uname);
    startActivity(intent);
    setContentView(R.layout.activity_home_page);
}

1 个答案:

答案 0 :(得分:0)

您可以使用Picasso库或Glide库来加载图像

Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);

上下文:是活动的上下文

http://i.imgur.com/DvpvklR.png :图片的网址

imageView :是您要在其中加载图片的ImageView