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" />
Ryan
  • 13
  • 1
  • 4
  • Any error occurs? Or your app crashed? Have you add internet permission to your manifest file? – Piyush Jan 25 '14 at 05:39
  • does this help? http://stackoverflow.com/questions/20292588/how-to-get-finish-callback-on-setimageurl-with-volley-library-and-networkimagevi – user109245 Jan 25 '14 at 05:39
  • yep and no errors in the logcat...is there otherway to debug where the error could be? – Ryan Jan 25 '14 at 05:42

3 Answers3

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>
InnocentKiller
  • 5,234
  • 7
  • 36
  • 84
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);


}
skyshine
  • 2,767
  • 7
  • 44
  • 84
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

Manmohan Badaya
  • 2,326
  • 1
  • 22
  • 35