从空格编码图像URL

时间:2014-02-20 12:53:34

标签: android url encoding bitmap special-characters

我正在开发一个RSS阅读器应用程序,但我遇到图像URL的问题,因为它们包含空格。我从这些论坛尝试了几种技术,但它们都给了我不同的错误。我尝试用%20替换空格,但我找不到文件异常。 这是我获取图像的方法:

//String ALLOWED_URI_CHARS = "@#&=*+-_.,:!?()/~'%";
            //url = Uri.encode(url, ALLOWED_URI_CHARS);
            //URL urlnew = new URL(url);
            //URI uri = new URI(urlnew.getProtocol(), urlnew.getUserInfo(), urlnew.getHost(), urlnew.getPort(), urlnew.getPath(), urlnew.getQuery(), urlnew.getRef());
            //urlnew = uri.toURL();
            //url = URLEncoder.encode(url.replace(" ", "%20"), "utf-8");
            url = url.replaceAll(" ", "%20");

            Bitmap bitmap=null;
            URL imageUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setRequestMethod("GET");
            Log.i("Code:",conn.getResponseCode()+" "+conn.getResponseMessage());
            Log.i("Error Message",conn.getErrorStream()+"");
            conn.setConnectTimeout(30000);
            conn.setReadTimeout(30000);
            conn.setInstanceFollowRedirects(true);
            InputStream is=conn.getInputStream();
            OutputStream os = new FileOutputStream(f);
            Utils.CopyStream(is, os);
            os.close();
            conn.disconnect();
            bitmap = decodeFile(f);
            return bitmap;

顶部的注释命令是我从其他人那里得到的一些命令。 logcat的:

02-20 13:58:19.015: W/System.err(29109): java.io.FileNotFoundException: http://ghadinews.net/upload/new/GhadiNews%20-%20parrot%20-%20ly.jpg
02-20 13:58:19.015: W/System.err(29109):    at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
02-20 13:58:19.015: W/System.err(29109):    at com.betaclass.ghadinews.ImageLoader.getBitmap(ImageLoader.java:94)
02-20 13:58:19.015: W/System.err(29109):    at com.betaclass.ghadinews.ImageLoader.access$0(ImageLoader.java:63)
02-20 13:58:19.015: W/System.err(29109):    at com.betaclass.ghadinews.ImageLoader$PhotosLoader.run(ImageLoader.java:168)
02-20 13:58:19.020: W/System.err(29109):    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:442)
02-20 13:58:19.020: W/System.err(29109):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
02-20 13:58:19.020: W/System.err(29109):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
02-20 13:58:19.020: W/System.err(29109):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
02-20 13:58:19.020: W/System.err(29109):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
02-20 13:58:19.020: W/System.err(29109):    at java.lang.Thread.run(Thread.java:856)

如果我将包含%20的编码网址插入浏览器,图片会正常打开,以便链接正常工作。

3 个答案:

答案 0 :(得分:2)

删除两者:

conn.setDoInput(true);
conn.setDoOutput(true);

希望这可以解决问题。

答案 1 :(得分:2)

String urlStr = "http://abc.dev.domain.com/0007AC/ads/800x480 15sec h.264.mp4";
URL url = new URL(urlStr);
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
url = uri.toURL();

相关:Bad URL encoding for images

答案 2 :(得分:0)

尝试这种技术

String query = URLEncoder.encode("apples oranges", "utf-8");
String url = "http://stackoverflow.com/search?q=" + query;

或使用类似

的内容
String uri = Uri.parse("http://...")
                .buildUpon()
                .appendQueryParameter("key", "val")
                .build().toString();