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

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值