android中的图片网址,根本不显示

时间:2014-01-25 05:36:35

标签: android url web imageview

我一直在寻找一些教程,或者至少是如何从网址加载图片的指南。 那么在当地它很容易..所以如果有人有建议或想法我可以开始,请分享。

我尝试了SmartImageView并在文档中跟进但仍然失败...... :(

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

这就是活动:

      <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" />

许可:是的

   <uses-permission android:name="android.permission.INTERNET" />

3 个答案:

答案 0 :(得分:0)

尝试以下代码。

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 :(得分:0)

试试这段代码 ImageView imageview;

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

代码:

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


}

答案 2 :(得分:0)

我已经尝试过SmartImageView并且它在1中运行。可能你的设备中没有互联网。我正在共享完整项目的驱动链接Follow this link to download