Android et l'affichage d'image
Bonjour,
Je me suis mit à android et j'ai déjà un problème qui reste sans réponse: l'affichage du image télécharger par le web, je n'arrive apparament pas à mettre en application les infos trouver sur le web
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| package com.example.projet.android;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import android.Manifest;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.widget.ImageView;
public class MainActivity extends Activity {
ImageView welcome;
URL urlWelcome=null;
@Override
public void onCreate(Bundle savedInstanceState) {
try {
urlWelcome= new URL("http://www.google.fr/intl/en_com/images/srpr/logo1w.png");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
welcome = (ImageView)findViewById(R.id.image_welcome);
try {
setImage(welcome,"http://www.google.fr/imgres?q=bienvenue&um=1&hl=fr&sa=N&biw=1366&bih=681&tbm=isch&tbnid=bN7f72VR4yA0QM:&imgrefurl=http://villefranchederouergue.parti-socialiste.fr/2012/01/12/bonjour-tout-le-monde/&docid=V32dQOWWxlvo8M&imgurl=http://villefranchederouergue.parti-socialiste.fr/files/2012/01/4dy88ime.gif&w=796&h=370&ei=uEaNUOmeOIaa1AXF7IHABg&zoom=1&iact=hc&vpx=186&vpy=159&dur=496&hovh=153&hovw=330&tx=264&ty=90&sig=100028177803817935053&page=2&tbnh=131&tbnw=262&start=17&ndsp=28&ved=1t:429,r:3,s:20,i:208");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@SuppressWarnings("unused")
public static void setImage(ImageView view, String url) throws IOException {
final URLConnection conn = new URL(url).openConnection();
conn.connect();
final InputStream is = conn.getInputStream();
final BufferedInputStream bis = new BufferedInputStream(is, 100000);
final Bitmap bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
view.setImageBitmap(bm);
}
} |
Layout:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/image_welcome"
android:contentDescription="@string/welcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RadioGroup
android:id="@+id/menu_group_button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/menu_route_button"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/menu_position_button" />
</RadioGroup>
<RadioGroup
android:id="@+id/menu_group_button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/menu_group_button1"
android:orientation="horizontal" >
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/menu_scan_button" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/menu_text_button"/>
</RadioGroup>
</RelativeLayout> |
Manifest:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.projet.android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest> |
Quelqu'un peut m'expliquer pourquoi mon image ne s'affiche pas?
Merci d'avance