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 68
|
public void readFromUrl(String _nomphoto) {
nomphoto=_nomphoto;
StringBuffer sb_p = new StringBuffer();
sb_p.append(urlphoto);
sb_p.append("nomphoto=");
sb_p.append(nomphoto);
//l'url du site web avec le nom de l'mage a telecharger
String httpUrl_p = sb_p.toString();
Log.i(getClass().getSimpleName(),"httpUrl_p = " + httpUrl_p);
try {
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 5000);
HttpClient httpclient = new DefaultHttpClient(params);
HttpGet httpGet = new HttpGet(httpUrl_p);
HttpResponse response = httpclient.execute(httpGet);
dataread = response.getEntity().getContent();
int bytesAvailable = dataread.available();
Log.i(getClass().getSimpleName(),"avant lecture bytes avail. = "+bytesAvailable);
EnregistrerImage(dataread);
} catch(Exception e) {
//gestion des exceptions
}
}
public void EnregistrerImage(InputStream data) throws NullPointerException, IOException {
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;
File file = new File("path_du_fichier"+nomphoto);
// create a buffer of maximum size
try {
bytesAvailable = data.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// read file and write it into form...
bytesRead = data.read(buffer, 0, bufferSize);
Log.i(getClass().getSimpleName(),"bytesRead="+bytesRead);
try {
FileOutputStream fos = new FileOutputStream(file);
while (bytesRead > 0) {
fos.write(buffer, 0, bufferSize);
bytesAvailable = data.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = data.read(buffer, 0, bufferSize);
Log.i(getClass().getSimpleName(),"bytesRead reste="+bytesRead);
}
fos.close();
} catch (java.io.IOException e) {
Log.e(getClass().getSimpleName(),"Exception in EnregistrerImage");
}
} catch (java.lang.NullPointerException e) {
Log.e(getClass().getSimpleName(),"Pointeur null");
}
} |
Partager