Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The code would probably work, alltough you are downloading your image on your main thread. This means that when it takes more than 5 seconds to download, you will be presented with the famous ANR dialog, and your app will crash...</p> <p>You should download your image in a background thread, and post the result back to your main thread. Back in the main thread, you can update your imageview with the downloaded image.</p> <p>Here's an example:</p> <pre><code>package nl.entreco.stackoverflow; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.net.URL; 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.AsyncTask; import android.os.Bundle; import android.util.Log; import android.widget.ImageView; public class StackOverflowActivity extends Activity { // private ImageView mImageView; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //Find the reference to the ImageView mImageView = (ImageView) findViewById(R.id.test_image); // You can set a temporary background here //image.setImageResource(null); // Start the DownloadImage task with the given url new DownloadImage().execute("http://i.imgur.com/CQzlM.jpg"); } /** * Simple functin to set a Drawable to the image View * @param drawable */ private void setImage(Drawable drawable) { mImageView.setBackgroundDrawable(drawable); } public class DownloadImage extends AsyncTask&lt;String, Integer, Drawable&gt; { @Override protected Drawable doInBackground(String... arg0) { // This is done in a background thread return downloadImage(arg0[0]); } /** * Called after the image has been downloaded * -&gt; this calls a function on the main thread again */ protected void onPostExecute(Drawable image) { setImage(image); } /** * Actually download the Image from the _url * @param _url * @return */ private Drawable downloadImage(String _url) { //Prepare to download image URL url; BufferedOutputStream out; InputStream in; BufferedInputStream buf; //BufferedInputStream buf; try { url = new URL(_url); in = url.openStream(); /* * THIS IS NOT NEEDED * * YOU TRY TO CREATE AN ACTUAL IMAGE HERE, BY WRITING * TO A NEW FILE * YOU ONLY NEED TO READ THE INPUTSTREAM * AND CONVERT THAT TO A BITMAP out = new BufferedOutputStream(new FileOutputStream("testImage.jpg")); int i; while ((i = in.read()) != -1) { out.write(i); } out.close(); in.close(); */ // Read the inputstream buf = new BufferedInputStream(in); // Convert the BufferedInputStream to a Bitmap Bitmap bMap = BitmapFactory.decodeStream(buf); if (in != null) { in.close(); } if (buf != null) { buf.close(); } return new BitmapDrawable(bMap); } catch (Exception e) { Log.e("Error reading file", e.toString()); } return null; } } } </code></pre> <p>Don't forget to add the permission.INTERNET to your manifest, if not, you will get an error...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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