使用OkHttp下载图像给出null

时间:2016-01-16 16:54:11

标签: android okhttp

我尝试使用OkHttp从URL下载图像。它返回null。

我有权限

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

这是我的Rest Client

 public class RestClient {

    private static volatile RestClient instance;
    private final OkHttpClient client;

    private RestClient(Context context) throws Exception {
        client = new OkHttpClient();
    }

    public static RestClient getInstance(Context context) throws Exception {
        if (instance == null)
            synchronized (RestClient.class) {
                if (instance == null)
                    instance = new RestClient(context);
            }
        return instance;
    }

    public Call uploadPicture(Callback callback){
        String link = "http://www.101apps.co.za/images/headers/101_logo_very_small.jpg";
        Request request = new Request.Builder()
                .url(String.format(link))
                .get()
                .build();
        Call call = client.newCall(request);
        call.enqueue(callback);
        return call;
    }
}

这是我在主要活动中的getPicture方法

   public void getPicByURL() {

        try {
            RestClient.getInstance(getActivity()).uploadPicture(new Callback() {

                @Override
                public void onFailure(Request request, IOException e) {
                    e.printStackTrace();
                }

                @Override
                public void onResponse(Response response) throws IOException {

                    final String json = response.body().string();
                    if (response.code() == Constants.SUCCESSFUL_DOWNLOAD) {
                        ResponseBody in = response.body();
                        InputStream inputStream = in.byteStream();
                        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
                                                updateImage(BitmapFactory.decodeStream(bufferedInputStream));
                    } 
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private void updateImage(final Bitmap bitmap) {

        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                if (bitmap != null) {
                    mSelectedPic.setImageBitmap(bitmap);
                } else {
                    Toast.makeText(getActivity(), R.string.show_error, Toast.LENGTH_SHORT).show();
                }
            }
        });
  }

updateImage()方法采用bitmap = null。我不知道自己做错了什么。请帮忙。

2 个答案:

答案 0 :(得分:2)

使用您当前的网址(http://www.101apps.co.za/images/headers/101_logo_very_small.jpg)和您的目的(仅将图片加载到ImageView中),我建议您改为使用PicassoGlide

使用Picasso时的示例代码:

        ImageView imageView = (ImageView) findViewById(R.id.imageView);
        Picasso.with(this)
                .load("http://www.101apps.co.za/images/headers/101_logo_very_small.jpg")
                .into(imageView);

希望它有所帮助!

答案 1 :(得分:1)

嗨,我的工作试试这个

  OkHttpClient client = new OkHttpClient();
  Request request = new Request.Builder().url("https://www.101apps.co.za/images/headers/101_logo_very_small.jpg").get().build();

            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    Log.i("Tag","error"+e.getMessage());
                }

               @Override
                public void onResponse(Call call, final Response response) throws IOException {

                   runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                   ImageView imageView (ImageView)findViewById(R.id.down_imageView);

                            ResponseBody in = response.body();
                            InputStream inputStream = in.byteStream();
                            Log.i("inputStream","inputstream value = "+inputStream);
                            // convert inputstram to bufferinoutstream
                            BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
                            Bitmap bitmap=BitmapFactory.decodeStream(bufferedInputStream);
                            Log.i("bitmap","bitmap value = "+bitmap);
                            imageView.setImageBitmap(bitmap);

                           }
                    });

               }
            });
        }
    });