[英]Loading image from URL in android
I am trying to load an image from a URL i get from a Volley JSONObjectRequest. 我正在尝试从我从Volley JSONObjectRequest获得的URL加载图像。
This is my JSON request: 这是我的JSON请求:
//creates the post request and posts
JsonObjectRequest getRandomImageURL = new JsonObjectRequest(Request.Method.POST, url_image,null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("ServerResponse","ATM ImageURL"+response.toString());
try {
String image = response.getString("Image");
Log.d("Image","ImageURL "+imgLink+image);
imgAd = (ImageView) findViewById(R.id.imgAd);
String imgURL = imgLink+image;
new DownloadImageTask(imgAd).execute(imgURL);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// error
Log.d("Error.Response","Error"+ error.getLocalizedMessage());
}
}
) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<>();
headers.put("Content-type", "application/json");
headers.put("Accept", "application/json");
headers.put("User-Agent", "Mozilla/5.0");
headers.put("Content-length", dataStream.toString());
return headers;
}
@Override
public byte[] getBody() {
try {
return mRequestBody == null ? null : mRequestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", mRequestBody, "utf-8");
return null;
}
}
};
Volley.newRequestQueue(this).add(getRandomImageURL);
}
And this is what i use to load the image from the URL: 这就是我用来从URL加载图像的内容:
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
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);
}
}
I keep on getting the following exception: 我继续收到以下异常:
W/System.err: java.io.FileNotFoundException: https://www.lamdacardservices.com/offers/CINEMA Logo.jpg
W/System.err: at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:197)
W/System.err: at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
W/System.err: at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:25)
W/System.err: at java.net.URL.openStream(URL.java:470)
W/System.err: at com.example.epavlos.lamda.map_activities.AtmMapActivity$DownloadImageTask$override.doInBackground(AtmMapActivity.java:327)
W/System.err: at com.example.epavlos.lamda.map_activities.AtmMapActivity$DownloadImageTask$override.access$dispatch(AtmMapActivity.java)
W/System.err: at com.example.epavlos.lamda.map_activities.AtmMapActivity$DownloadImageTask.doInBackground(AtmMapActivity.java:0)
W/System.err: at com.example.epavlos.lamda.map_activities.AtmMapActivity$DownloadImageTask.doInBackground(AtmMapActivity.java:317)
W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:288)
W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
W/System.err: at java.lang.Thread.run(Thread.java:818)
Can anyone help me on what i am doing wrong? 谁能帮我解决我做错的事情? Or even if there is an easier way to achieve this, maybe using volley or something?
或者,即使有更简单的方法可以做到这一点,例如使用凌空抽球等?
Try this: 尝试这个:
String temp = "https://www.lamdacardservices.com/offers/CINEMA Logo.jpg";
temp = temp.replaceAll(" ", "%20");
URL sourceUrl = new URL(temp);
https://www.lamdacardservices.com/offers/CINEMA Logo.jpg https://www.lamdacardservices.com/offers/CINEMA Logo.jpg
Check your URL it contains space between image name try to use URL Encoding 检查您的URL,其中图像名称之间包含空格。尝试使用URL编码
https://www.lamdacardservices.com/offers/CINEMA%20Logo.jpg https://www.lamdacardservices.com/offers/CINEMA%20Logo.jpg
try with this URL and let me know. 尝试使用此URL,让我知道。
Just replace your space with %20
只需用
%20
替换您的空间
You should encode URL that will handle problems like. 您应该对将处理类似问题的URL进行编码。 One way is
一种方法是
Uri.encode("https://www.lamdacardservices.com/offers/CINEMA Logo.jpg")
Use Glide for this, One of the best third party library 为此,使用Glide,最好的第三方库之一
https://github.com/bumptech/glide https://github.com/bumptech/glide
You may also opt to use ImageLoader as a powerful image loading and caching tool. 您也可以选择使用ImageLoader作为功能强大的图像加载和缓存工具。
Gradle dependency : Gradle依赖关系:
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5' 编译'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
To use : 使用方法:
ImageLoader imageLoader = ImageLoader.getInstance(); ImageLoader imageLoader = ImageLoader.getInstance(); // Get singleton instance
//获取单例实例
imageLoader.displayImage(imageUri, imageView); imageLoader.displayImage(imageUri,imageView);
Source: https://github.com/nostra13/Android-Universal-Image-Loader 来源: https : //github.com/nostra13/Android-Universal-Image-Loader
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.