1

この URL を解析する方法

http://ioe.edu.np/exam/notices/8560Result%20Diploma%20I_I.jpg

imageview のソースとして使用できるようにします。Uri.encode() を使用してエンコードしようとしましたが、それは役に立ちませんでした。

以下は、URLから画像をロードするために参照しているコードです。Androidから取得し、ImageView の画像と同じ URL に画像を作成します

public class MainActivity extends Activity {

//  String imageUrl1 = "http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png";

String imageUrl = Uri.encode("http://ioe.edu.np/exam/notices/8560Result Diploma I_I.jpg");

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    try {
        ImageView i = (ImageView) findViewById(R.id.imageView1);
        Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(
                imageUrl).getContent());
        i.setImageBitmap(bitmap);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

}

私を助けてください。また、画像ファイルの名前を制御できないことに注意してください。そのため、画像ビューに正しく読み込まれるようにする方法を見つける必要があります。imageUrl を imageUrl1 に置き換えると、画像が読み込まれます。しかし、imageUrl では、スペースが html エンティティにエンコードされることに問題があるようです。これで私を助けてください。

ありがとうございました。

4

2 に答える 2

1

使用する

String imageUrl ="http://ioe.edu.np/exam/notices/8560Result Diploma I_I.jpg";
    imageUrl =imageUrl .replaceAll(" ", "%20");

サンプルコード

String imageUrl ="http://ioe.edu.np/exam/notices/8560Result Diploma I_I.jpg";
    imageUrl =imageUrl .replaceAll(" ", "%20");
    try {
        Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(
                imageUrl).getContent());

        ImageView im=new ImageView(this);
        im.setImageBitmap(bitmap);
        setContentView(im);
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
于 2012-07-16T18:27:32.113 に答える
0

このコードは私の場合に機能します。

ImageView i = (ImageView) findViewById(R.id.imageView1);

try {
        URL url = new URL("http://ioe.edu.np/exam/notices/8560Result%20Diploma%20I_I.jpg");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        i.setImageBitmap(myBitmap);
    } catch (IOException e) {
        e.printStackTrace();
    }

また、マニフェスト ファイルに権限を追加し、

<uses-permission android:name="android.permission.INTERNET"/>

編集:

も使えますUrlEncoder

String urlString = URLEncoder.encode("http://ioe.edu.np/exam/notices/8560Result Diploma I_I.jpg");
url = new URL(urlString);
于 2012-07-16T18:26:50.703 に答える