1

Androidアプリケーションで画像を表示する必要があります。問題は、この画像をオンラインのどこかから取得したいということです。URLは次のようになります。

http://www.mydomain.com/hello world image.png

ご覧のとおり、画像名にいくつかのスペースが含まれています。コードを実行するたびに、FileNotFoundの例外が表示されます。そして何も起こりません。

以下は私のコードです

String imagePathCon = "hello world image.png";      
String imagePath = "http://www.mydomain.com/" + imagePathCon;
try {
  URL url;
  url = new URL(imagePath);

  // url = new URL("http://www.azuma-kinba.com/wp-content/uploads/2012/05/Android-Make-Google-Loss-in-2010.png");

  InputStream content = (InputStream)url.getContent();
  Drawable d = Drawable.createFromStream(content , "src"); 
  im.setImageDrawable(d);

 } catch (MalformedURLException e) {
            e.printStackTrace();
 } catch (IOException e) {
            e.printStackTrace();
 }

スペースを「+」に置き換えるために使用しますが、何も起こりません。

4

4 に答える 4

3

画像を取得するために正しい URL を使用し、以下のコードを使用してください。

を使用して URL のスペースを置き換えます...

  imagePath=imagePath.replaceAll(" ", "%20");

そしていま...

         HttpGet httpRequest = new HttpGet(new URL(params[0]).toURI());
         HttpClient httpClient = new DefaultHttpClient();
         HttpResponse response = (HttpResponse) httpClient.execute(httpRequest);
         HttpEntity entity = response.getEntity();
         BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity); 
             InputStream is = bufHttpEntity.getContent();
        //image_value = new URL("image Url is here");
            bm = BitmapFactory.decodeStream(new FlushedInputStream(is));
    //imageLoader is object of iamge view
           imageLoader.setImageBitmap(bm);
于 2012-06-08T07:05:55.200 に答える
3

これは機能します。

String imagePathCon = "hello world image.png";   
imagePathCon=imagePathCon.replaceAll(" ", "%20");

String imagePath = "http://www.mydomain.com/" + imagePathCon;

http://en.wikipedia.org/wiki/Percent-encoding#Character_dataを知っておく必要があります

于 2012-06-08T07:03:44.020 に答える
0

次のように、パスをURLエンコードする必要があります。

String imagePathCon = URLEncoder.encode("hello world image.png", "UTF-8");
于 2012-06-08T06:58:49.663 に答える
0

http get リクエストを使用することもできます。最も簡単な方法は、文字列内のすべての文字を URL エンコード形式に自動的に置き換える URLEncoder を呼び出すことです。

HttpParams httpParameters = new BasicHttpParams(); 
HttpConnectionParams.setConnectionTimeout(httpParameters, 30000); 
HttpConnectionParams.setSoTimeout(httpParameters, 30000);
HttpClient httpclient = new DefaultHttpClient(httpParameters); 
HttpGet httpGet = new HttpGet(URLEncoder.encode(url, "UTF-8"));
HttpResponse httpResponse = httpclient.execute(httpGet); 
in = httpResponse.getEntity().getContent(); 
//Bitmap bmp = BitmapFactory.decodeStream(instream); 
//create a bitmap or an image from the input stream
于 2012-06-08T07:08:22.757 に答える