Android 使用OkHttp下载图像时将显示null
我尝试使用OkHttp从URL下载图像。它返回null 我有权限Android 使用OkHttp下载图像时将显示null,android,okhttp,Android,Okhttp,我尝试使用OkHttp从URL下载图像。它返回null 我有权限 <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 下面是我在主要活动中的getPicture方法 public void getPicByURL() { try
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
下面是我在主要活动中的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()
方法接受位图=null。我不知道我做错了什么。请帮助。使用您当前的Url(http://www.101apps.co.za/images/headers/101_logo_very_small.jpg
)和您的目的(仅将图像加载到ImageView中),我建议您使用或
使用毕加索时的示例代码:
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);
希望有帮助 与您当前的Url(http://www.101apps.co.za/images/headers/101_logo_very_small.jpg
)和您的目的(仅将图像加载到ImageView中),我建议您使用或
使用毕加索时的示例代码:
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);
希望有帮助 嗨,这对我有用试试这个
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);
}
});
}
});
}
});
嗨,这对我有用试试这个
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);
}
});
}
});
}
});
你查过网址了吗?在borwser中打开你的url会给我404而不是found@Raghunandan-是的。另一个URL返回空值,该图像URL(
http://www.101apps.co.za/images/headers/101_logo_very_small.jpg
)为什么不使用或?是否检查了url?在borwser中打开你的url会给我404而不是found@Raghunandan-是的。另一个URL返回空值,该图像URL(http://www.101apps.co.za/images/headers/101_logo_very_small.jpg
)为什么不使用或?