Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The link you mention already give answer ! Just use that method. It should be like this. Two important things ! Add <strong>Internet Permission</strong> in <code>AndroidManifest.xml</code>. Apply <strong>Strict Mode</strong> in UI Create. See below</p> <p><strong>MainActivity.java</strong></p> <pre><code>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 =&gt; 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; } } } </code></pre> <p><strong>activity_main.xml</strong></p> <pre><code>&lt;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" &gt; &lt;/RelativeLayout&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload