get image from url and set as relative layout background on android

Vio Mugen picture Vio Mugen · Nov 5, 2013 · Viewed 13.7k times · Source

basically what I want to do is, use the url of an image (it doesnt matter if it is downloaded or not) to set the background of relativelayout

I have googled for hours and tried a lot of suggestions but I always end up with errors and app crashes...

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

I tried others like this (http://looksok.wordpress.com/2013/07/06/android-tutorial-download-image-from-the-internet-with-url/), but it didnt work

Answer

Thein picture Thein · Nov 5, 2013

The link you mention already give answer ! Just use that method. It should be like this. Two important things ! Add Internet Permission in AndroidManifest.xml. Apply Strict Mode in UI Create. See below

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>