android从服务器下载文件(php+apache+win7+MySql)
16lz
2022-03-07
举例: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
更多相关文章
- Android打开通讯录并获取数据
- 2011.09.26(3)——— android sample之Notepad(带下划线的TestView
- 面试例题6:两种方法将图像显示在View上
- Android常用方法——获取系统信息
- android xml
- android获取手机型号和系统版本号
- Android(安卓)HorizontalScrollView 水平滑动 在listview上面动
- Android(安卓)使用Gallery仿3D画廊效果实现
- Android媒体文件metadata是如何实现跟解码器结合获取的