Essayez quelque chose comme ceci
String image_URL="http://chart.apis.google.com/chart?chs=200x200&cht=qr&chl=http%3A%2F%2Fandroid-er.blogspot.com%2F";
String extStorageDirectory;
File file;
Bitmap bm;
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)){
file=new File(android.os.Environment.getExternalStorageDirectory(),"Your FolderName");
extStorageDirectory = Environment.getExternalStorageDirectory().toString();
}else{
file=YourActivity.this.getCacheDir();
}
if(!file.exists())
file.mkdirs();
extStorageDirectory+="Your FolderName/yourimagename.PNG";
File imageFile = new File(extStorageDirectory);
Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
if(bitmap!=null){
imageview.setImageBitmap(bitmap);
}else{
extStorageDirectory = Environment.getExternalStorageDirectory().toString();
BitmapFactory.Options bmOptions;
bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 1;
bm = LoadImage(image_URL, bmOptions);
imageview.setImageBitmap(bm);
OutputStream outStream = null;
file=new File(android.os.Environment.getExternalStorageDirectory(),"Your FolderName");
file=new File(extStorageDirectory, "Your FolderName/yourimagename.PNG");
try {
outStream = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
où les méthodes sont les suivantes
private Bitmap LoadImage(String URL, BitmapFactory.Options options)
{
Bitmap bitmap = null;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in, null, options);
in.close();
} catch (IOException e1) {
}
return bitmap;
}
private InputStream OpenHttpConnection(String strURL) throws IOException{
InputStream inputStream = null;
URL url = new URL(strURL);
URLConnection conn = url.openConnection();
try{
HttpURLConnection httpConn = (HttpURLConnection)conn;
httpConn.setRequestMethod("GET");
httpConn.connect();
if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
inputStream = httpConn.getInputStream();
}
}catch (Exception ex){
Log.e("error",ex.toString());
}
return inputStream;
}