1

基本上我想要做的是,使用图片的url(是否下载无关紧要)来设置relativelayout的背景

我已经搜索了几个小时并尝试了很多建议,但我总是以错误和应用程序崩溃而告终......

if((!modelsArrayList.get(position).isGroupHeader())&&       (modelsArrayList.get(position).isProfBox())) {
            rowView = inflater.inflate(R.layout.prof_header, parent, false);
            TextView titleView = (TextView)     rowView.findViewById(R.id.header);
            titleView.setText(modelsArrayList.get(position).getTitle());
            RelativeLayout rLayout=(RelativeLayout)rowView.findViewById(R.id.ProfHead);
           rLayout.setBackgroundResource("HTTP://URL.HERE");

我试过这样的其他人(http://looksok.wordpress.com/2013/07/06/android-tutorial-download-image-from-the-internet-with-url/),但它没有用

4

2 回答 2

4

您提到的链接已经给出了答案!就用那个方法。应该是这样的。两件重要的事!在 中添加Internet 权限AndroidManifest.xml。在 UI Create 中应用严格模式。见下文

MainActivity.java

package com.example.imagedownload;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.widget.RelativeLayout;

public class MainActivity extends Activity {

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

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);

    // About StrictMode Learn More at => http://stackoverflow.com/questions/8258725/strict-mode-in-android-2-2
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();      
    StrictMode.setThreadPolicy(policy);

    Bitmap myImage = getBitmapFromURL("http://looksok.files.wordpress.com/2011/12/me.jpg");

    RelativeLayout rLayout=(RelativeLayout)findViewById(R.id.relativeLayout);

    //BitmapDrawable(obj) convert Bitmap object into drawable object.
    Drawable dr = new BitmapDrawable(myImage);
    rLayout.setBackgroundDrawable(dr);


    return true;
}

public Bitmap getBitmapFromURL(String imageUrl) {
    try {
        URL url = new URL(imageUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" 
>

</RelativeLayout>
于 2013-11-05T09:33:26.437 回答
1

如果您尚未在清单文件中添加此代码段,请在清单中启用 Internet 权限。可以在这里找到Project_Name\app\src\main\AndroidManifest.xml

笔记

LINK_TO_THE_IMAGE将链接到您的图片

YOUR_LAYOUT将是您的布局的名称

在您的清单中

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

然后在你的java代码中

     try {
            URL url = new URL(LINK_TO_THE_IMAGE);
            Bitmap bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());
            Drawable image = new BitmapDrawable(context.getResources(), bitmap);

            YOUR_LAYOUT.image.setBackground(image);

        } catch (Exception e) {
            e.printStackTrace();
        }
于 2018-09-20T19:19:26.197 回答