1

I'm been looking for some tutorials or atleast a guide how to load an image from url. Well locally it's quite pretty easy.. So if anyone has suggestions or an idea where I can start, please share.

I tried the SmartImageView and followed in the documentation but still failed...:(

        protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    image = (ImageView)findViewById(R.id.imageView1);
    text = (TextView)findViewById(R.id.textView1);
    text.setText("Show image by url");

    SmartImageView myImage = (SmartImageView) this.findViewById(R.id.my_image);
    myImage.setImageUrl("http://icons.iconarchive.com/icons/yellowicon/game-stars/256/Mario-icon.png");
    text.setText("Status: " + myImage);
}

And this on the activity:

      <com.loopj.android.image.SmartImageView
     android:id="@+id/my_image"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignLeft="@+id/textView1"
     android:layout_below="@+id/imageView1"
     android:layout_marginLeft="14dp"
     android:layout_marginTop="37dp" />

Permission : yes

   <uses-permission android:name="android.permission.INTERNET" />
3
  • Any error occurs? Or your app crashed? Have you add internet permission to your manifest file?
    – Piyush
    Jan 25, 2014 at 5:39
  • does this help? stackoverflow.com/questions/20292588/…
    – user109245
    Jan 25, 2014 at 5:39
  • yep <uses-permission android:name="android.permission.INTERNET" /> and no errors in the logcat...is there otherway to debug where the error could be?
    – Ryan
    Jan 25, 2014 at 5:42

3 Answers 3

0

Try below code.

public class ImageFromUrlExample extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

ImageView imgView =(ImageView)findViewById(R.id.ImageView01);
Drawable drawable = LoadImageFromWebOperations("your-server-link/android.png");
imgView.setImageDrawable(drawable);

}

private Drawable LoadImageFromWebOperations(String url)
{
try
{
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
}catch (Exception e) {
System.out.println("Exc="+e);
return null;
}
}
}

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView android:id="@+id/ImageView01"
android:layout_height="wrap_content" android:layout_width="wrap_content"/>
</LinearLayout>
1
  • Also add <uses-permission android:name="android.permission.INTERNET" /> this permission to manifest file. Jan 25, 2014 at 5:41
0

try this code ImageView imageview;

new DownloadImageTask(imageview)  .execute("http://icons.iconarchive.com/icons/yellowicon/game-stars/256/Mario-icon.png);

code:

public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;

public DownloadImageTask(ImageView bmImage) {
    this.bmImage = bmImage;

}
@Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

protected Bitmap doInBackground(String... urls) {
    String urldisplay = urls[0];
    Bitmap mIcon11 = null;
    try {
        InputStream in = new java.net.URL(urldisplay).openStream();
        mIcon11 = BitmapFactory.decodeStream(in);
    } catch (Exception e) {
        Log.e("Error", e.getMessage());
        e.printStackTrace();
    }
    return mIcon11;
}

protected void onPostExecute(Bitmap result) {


    bmImage.setImageBitmap(result);
    bmImage.setVisibility(View.VISIBLE);
    super.onPostExecute(result);


}
3
  • Hi Peter, I tried your code, but then my app crashed...Tried with a new project and still the same. Am I missing something?
    – Ryan
    Jan 25, 2014 at 6:30
  • I also started the project on Github : github.com/Lioncode/ImageViewUrl
    – Ryan
    Jan 25, 2014 at 6:35
  • what error you are getting,internet connection added or not
    – skyshine
    Jan 25, 2014 at 7:08
0

I have tried SmartImageView and it ran in 1 go.might b u have not internet in ur device.i am sharing drive link of complete project Follow this link to download

5
  • Ok that seems to work! :)
    – Ryan
    Jan 25, 2014 at 6:47
  • change android platform ver from project prop .hope it helped. Jan 25, 2014 at 6:47
  • Yeah it work, but just wondering what did I miss?
    – Ryan
    Jan 25, 2014 at 6:50
  • u can share similar yours i'll try to tell u . u can accept this as well.. Jan 25, 2014 at 6:51
  • Is it possible because I didn't add it correctly in my library...
    – Ryan
    Jan 25, 2014 at 6:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.