从url获取Bitmap的挑战

时间:2016-09-23 12:51:13

标签: android android-bitmap

我知道这个问题有很多答案,但这是一个挑战,我只想让一个人尝试一下。

http://api.pt-helper.com/ptimages/web/uploads/Median%20Nerve%20Glide%20–%20D%202_300.png

上面是需要在ImageView中获取并显示的图像网址。问题是图像没有适当地从URL加载。

我想发布wt很有挑战性,因为 zgc7009 已经设法找到正确的答案并了解我的Q.

  • 在上面的网址中,字符不是一个超级但是EN DASH,其末尾附近有十六进制代码e2 80 93。所以它在浏览器上运行,但无法在Androi设备上加载。好Glide确实加载了它,我必须说它是一个了不起的库。

感谢大家花时间试图解决这个问题。

4 个答案:

答案 0 :(得分:1)

我正在使用滑动库从URL获取图像。我刚刚使用了下面的代码

 Glide.with(activityRef)
                        .load("http://api.pt-helper.com/ptimages/web/uploads/Median%20Nerve%20Glide%20–%20D%202_300.png")
                        .diskCacheStrategy(DiskCacheStrategy.NONE)
                        .skipMemoryCache(true)                   
                        .crossFade()
                        .override(1700, 1700)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .into(viewHolder.feedImage);

            }

在不到一秒钟的时间里,我得到了这张照片!

答案 1 :(得分:0)

使用Volley's ImageRequest从网址获取图片

    public static final String URL = "http://api.pt-helper.com/ptimages/web/uploads/Median%20Nerve%20Glide%20–%20D%202_300.png";

    ImageRequest imageRequest = new ImageRequest(URL, new Response.Listener<Bitmap>() {
        @Override
        public void onResponse(final Bitmap response) {

                    your_imageView.setImageBitmap(response);
        }
    }, 0, 0, ImageView.ScaleType.CENTER_INSIDE, null, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(your_activity_name.this, error.toString(), Toast.LENGTH_LONG).show();
        }
    });

    RequestQueue requestQueue = Volley.newRequestQueue(RescuingActivity.this);
    requestQueue.add(imageRequest);

答案 2 :(得分:0)

如果重图片使用background thread

将其加载到AsyncTask

注意:确保您在清单文件中添加了互联网权限。

public class LoadImageWebActivity extends Activity {
    /** Called when the activity is first created. */

    ImageView imgLogo;
    ProgressBar progressbar;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        imgLogo = (ImageView) findViewById(R.id.imageView1);
        progressbar = (ProgressBar) findViewById(R.id.loadingBar);

    } 

    public void btnLoadImageClick(View view)
    {
        //imgLogo.setBackgroundDrawable(LoadImageFromWeb("http://www.android.com/media/wallpaper/gif/android_logo.gif"));
        new loadImageTask().execute("http://api.pt-helper.com/ptimages/web/uploads/Median%20Nerve%20Glide%20–%20D%202_300.png");
    }

    public class loadImageTask extends AsyncTask<String, Void, Void>
    {
        Drawable imgLoad;

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();

            progressbar.setVisibility(View.VISIBLE);
        }

        @Override
        protected Void doInBackground(String... params) {
            // TODO Auto-generated method stub

            imgLoad = LoadImageFromWeb(params[0]);
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);

            if(progressbar.isShown())
            {
                progressbar.setVisibility(View.GONE);
                imgLogo.setVisibility(View.VISIBLE);
                imgLogo.setBackgroundDrawable(imgLoad);
            }
        }
    }

    public static Drawable LoadImageFromWeb(String url) 
    {
        try 
        {
            InputStream is = (InputStream) new URL(url).getContent();
            Drawable d = Drawable.createFromStream(is, "src name");
            return d;
        } catch (Exception e) {
            return null;
        }
    }
}

<强>布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <Button 
        android:text="Load Image" 
        android:id="@+id/button1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:onClick="btnLoadImageClick">
    </Button>

  <RelativeLayout
    android:id="@+id/sublayout"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:layout_below="@+id/button1">

      <ProgressBar
        android:id="@+id/loadingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:visibility="gone">
    </ProgressBar>

    <ImageView 
        android:id="@+id/imageView1" 
        android:layout_height="200dp" 
        android:layout_width="200dp"
        android:scaleType="center"
        android:layout_centerInParent="true">
    </ImageView>
  </RelativeLayout>

</RelativeLayout>

答案 3 :(得分:0)

我收回了我评论的一些的严酷性:P你提供的网址确实在浏览器中显示了一个图像,但我无法用Picasso库加载图像提供的网址。然而,在检查元素并利用图像本身内的图像源的URL后,恰好是

http://api.pt-helper.com/ptimages/web/uploads/Median%20Nerve%20Glide%20%E2%80%93%20D%202_300.png

我能用Picasso拉出图像。

Screenshot of successful pull