package com.jshop.android.util;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.http.util.EncodingUtils;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
public class BaseTools extends Activity {
/**
* 写入一个默认的餐桌值,用来检测未就做即可点菜的问题
* @param content
*/
public void writeJmtable(String content){
try{
//实例化文件文件输出流
FileOutputStream fos=openFileOutput(JshopMParams.SHAREMTABLEPARAM,MODE_WORLD_WRITEABLE+MODE_WORLD_WRITEABLE);
fos.write(content.getBytes());
fos.close();
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 读取餐桌信息文件
* @return
*/
public String readJmtable(){
String res="";
try{
FileInputStream fis=openFileInput(JshopMParams.SHAREMTABLEPARAM);
byte[]buffer=new byte[fis.available()];
fis.read(buffer);
res=EncodingUtils.getString(buffer,"UTF-8");
fis.close();
}catch(Exception e){
e.printStackTrace();
}
return res;
}
/**
* 读取服务器地址
*/
public void gethosturl(){
String oserverhost=read();
if(oserverhost!=null){
//放入静态变量
JshopActivityUtil.BASE_URL="http://"+oserverhost;
}
}
/**
* 读取服务器地址文件
* @return
*/
public String read(){
String res="";
try{
FileInputStream fis=openFileInput(JshopMParams.FILENAME);
byte[]buffer=new byte[fis.available()];
fis.read(buffer);
res=EncodingUtils.getString(buffer,"UTF-8");
fis.close();
}catch(Exception e){
e.printStackTrace();
}
return res;
}
/**
* 写文件,保存服务器地址
* @param content
*/
public void write(String content){
try{
//实例化文件文件输出流
FileOutputStream fos=openFileOutput(JshopMParams.FILENAME,MODE_WORLD_WRITEABLE+MODE_WORLD_WRITEABLE);
fos.write(content.getBytes());
fos.close();
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 获取网络图片
* @param pictureurl
* @return
* @throws IOException
*/
public Bitmap getPictureurlImg(String pictureurl) throws IOException{
URL url=new URL(pictureurl);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5*1000);
InputStream in=conn.getInputStream();
Bitmap bm=BitmapFactory.decodeStream(in);
in.close();
return bm;
}
public static Drawable resizeImage(Bitmap bitmap,int w,int h){
// load the origial Bitmap
Bitmap BitmapOrg = bitmap;
int width = BitmapOrg.getWidth();
int height = BitmapOrg.getHeight();
int newWidth = w;
int newHeight = h;
// calculate the scale
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the Bitmap
matrix.postScale(scaleWidth, scaleHeight);
// if you want to rotate the Bitmap
// matrix.postRotate(45);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width,
height, matrix, true);
// make a Drawable from Bitmap to allow to set the Bitmap
// to the ImageView, ImageButton or what ever
return new BitmapDrawable(resizedBitmap);
}
}