android从服务器下载文件(php+apache+win7+MySql)
举例:android从apache服务器获取文件(图像),保存到相应的目录中,并从目录中将图像转变为Bitmap
/* * 参数 :将要获取的服务器文件名 filename * 如果成功,返回真*/boolean getImageByWeb2(String filename) { File img = new File(TEMP_WEB_IMAGE_PATH + filename); // Create directories new File(TEMP_WEB_IMAGE_PATH).mkdirs(); // only download new images if (!img.exists()) { try { URL imageUrl = new URL("http://10.10.145.154/WebImageProcess/output/" + filename); InputStream in = imageUrl.openStream(); OutputStream out = new BufferedOutputStream(new FileOutputStream(img)); for (int b; (b = in.read()) != -1;) { out.write(b); } out.close(); in.close(); } catch (MalformedURLException e) { img = null; return false; } catch (IOException e) { img = null; return false; } } return true; }
调用:
if(getImageByWeb2("processed_"+serverFileName)){ //将保存完毕的图像,转变为Bitmap格式 Bitmap resultForWebImage=FilesToBitmap(TEMP_WEB_IMAGE_PATH+"processed_"+serverFileName);}
附属代码:
private static final String TEMP_WEB_IMAGE_PATH="/sdcard/app/tmp/";Bitmap FilesToBitmap(String filename){ Bitmap temp=null; if(filename!=null){ File imageFile = new File(filename); if (imageFile.exists()) { // Load the image from file temp = BitmapFactory.decodeFile(filename); } } return temp;}
转载请声明:http://blog.csdn.net/nuptboyzhb/article/details/7983318