我正在尝试从网址获取图片并加载imageView
,但我无法对网址字符串进行编码,请告诉我如何编码。
请找到代码&网址用于您的参考。
String link=URLEncoder.encode(urlStr);
bitmap=loadBitmap(link);
public static Bitmap loadBitmap(String url) {
Bitmap bitmap = null;
try {
InputStream in = new java.net.URL(url).openStream();
bitmap = BitmapFactory.decodeStream(in);
} catch (Exception e) {
}
return bitmap;
}
答案 0 :(得分:1)
使用此代码,它将从服务器
获取图像String URL = "http://images.pcmac.org/SiSFiles/Schools/TN/JacksonMadisonCounty/RoseHillMiddle/Uploads/Locations/%7BB690E93E-F7C9-48AF-B72A-BFF944FA6D4A%7D_104_9169.JPG";
private InputStream OpenHttpConnection(String urlString) throws IOException {
InputStream in = null;
int response = -1;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");
try {
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
} catch (Exception ex) {
throw new IOException("Error connecting");
}
return in;
}
private Bitmap DownloadImage(String URL) {
Bitmap bitmap = null;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
final String msg = e1.getMessage();
handler.post(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), "" + msg, Toast.LENGTH_SHORT).show();
}
});
}
return bitmap;
}
答案 1 :(得分:1)
仅供参考,URLEncoder.encode(String)已被弃用。
参考: http://developer.android.com/reference/java/net/URLEncoder.html
答案 2 :(得分:0)
您可以按如下方式对网址进行编码:
String URL = "http://images.pcmac.org/SiSFiles/Schools/TN/JacksonMadisonCounty/RoseHillMiddle/Uploads/Locations/%7BB690E93E-F7C9-48AF-B72A-BFF944FA6D4A%7D_104_9169.JPG"; String encodeUrl=URLEncoder.encode(URL, "utf-8"); private Bitmap DownloadImage(String URL) { Bitmap bitmap = null; InputStream in = null; try { in = OpenHttpConnection(URL); bitmap = BitmapFactory.decodeStream(in); in.close(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); final String msg = e1.getMessage(); handler.post(new Runnable() { public void run() { Toast.makeText(getApplicationContext(), "" + msg, Toast.LENGTH_SHORT).show(); } }); } return bitmap; }
答案 3 :(得分:0)
请使用以下代码下载并将图像显示到imageview。
public class MainActivity extends Activity {
ImageView my_img;
Bitmap mybitmap;
ProgressDialog pd;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView mImgView1 = (ImageView) findViewById(R.id.mImgView1);
DownloadImageFromURL mDisImgFromUrl = new DownloadImageFromURL(mImgView1);
mDisImgFromUrl.execute("http://images.pcmac.org/SiSFiles/Schools/TN/JacksonMadisonCounty/RoseHillMiddle/Uploads/Locations/%7BB690E93E-F7C9-48AF-B72A-BFF944FA6D4A%7D_104_9169.JPG");
}
private class DownloadImageFromURL extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pd = new ProgressDialog(MainActivity.this);
pd.setMessage("Loading...");
pd.show();
}
public DownloadImageFromURL(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);
pd.dismiss();
}
}
}
答案 4 :(得分:0)
我为How to display image from URL创建了示例。使用该示例它适用于我,我也通过替换你的图像网址来检查它。