//操作SDCARD的文件工具类
public class FileUtil {
//存放图片的路径
private static final String CACHE_DIR = Environment.getExternalStorageDirectory() + "/my_caches/images";
private static int COMP_JPG = 0;
private static int COMP_PNG = 1;
//判断sdcard 是否挂载(是否有sdcard)
public static boolean isMounted(){
//获取手机设备的sdcard状态
String state = Environment.getExternalStorageState();
//MOUNTED -- 安装
return state.equals(Environment.MEDIA_MOUNTED);
}
//获取sdcard文件 根路径的绝对路径
public static String getSDCARD(){
return Environment.getExternalStorageDirectory().getAbsolutePath();
}
//获取文件名
public static String getFilename(String url){
return url.substring(url.lastIndexOf('/') + 1);
}
//保存文件 方法1
public static void sava1(String url,byte[] data){
//判断是否有sdcard
if(!isMounted()){
return ;
}
//有sdcard
//判断是否有缓存文件夹
File dir = new File(CACHE_DIR);
if(!dir.exists()){
//不存在缓存文件夹 创建文件夹用来保存文件
dir.mkdirs();
}
//把文件 数据存到sdcard
File file = new File(dir,getFilename(url));
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(data);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//保存文件 方法2
public static void sava2(String url,Bitmap bitmap,int format){
//判断 是否有sdcard
if(!isMounted()){
return ;
}
File dir = new File(CACHE_DIR);
if(!dir.exists()){
dir.mkdirs();
}
//把 文件数据 写到 sdcard
File file = new File(dir,getFilename(url));
try {
FileOutputStream fos = new FileOutputStream(file);
//把图片文件写入缓存
bitmap.compress((format == COMP_JPG?CompressFormat.JPEG:CompressFormat.PNG), 100, fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//读取图片
public static Bitmap readImage(String url){
if(!isMounted()){
return null;
}
File file = new File(CACHE_DIR,getFilename(url));
if(file.exists()){
return BitmapFactory.decodeFile(file.getAbsolutePath());
}
return null;
}
//清空 缓存目录
public void clearCaches(){
File dir = new File(CACHE_DIR);
File[] file_datas = dir.listFiles();
for(File file : file_datas){
file.delete();
}
}
//生成略缩图
public static Bitmap creatThumbnail(String filepath,int newWidth,int newHeight){
BitmapFactory.Options options = new BitmapFactory.Options();
//
options.inJustDecodeBounds = true;
//目的:不生成bitmap数据,但是要获取原图的参数值
BitmapFactory.decodeFile(filepath, options);
//原有宽高
int originalWidth = options.outWidth;
int originalHeight = options.outHeight;
//按比例缩小的宽高
int width = originalWidth/newWidth;
int height = originalHeight/newHeight;
options.inSampleSize = height > width? height : width;
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(filepath, options);
}
}
===========================
//sql数据库增删改查工具类
public class Sqlite_operate_utils {
private static String DB_PATH = Environment.getExternalStorageDirectory() + File.separator + "studentinfo2.db";
private SQLiteDatabase db;
//构造函数 new 该类的时候 就去找 需要找的 数据库
public Sqlite_operate_utils() {
db = SQLiteDatabase.openDatabase(DB_PATH, null, SQLiteDatabase.OPEN_READWRITE);
}
//查询 数据的 方法1
public Cursor sqlite_select(String content, String[] condition){
return db.rawQuery(content, condition);
}
//查询 数据 的 方法2
public List<Map<String, String>> sqlite_selectlist(String content, String[] condition){
Cursor cursor = db.rawQuery(content, condition);
return cursorToList(cursor);
}
//返回List
public List<Map<String, String>> cursorToList(Cursor cursor) {
List<Map<String, String>> list = new ArrayList<Map<String,String>>();
while(cursor.moveToNext()){//数据库表的 行
Map<String, String> map = new HashMap<String, String>();
for(int i = 0;i<cursor.getColumnCount();i++){//数据库表的列
map.put(cursor.getColumnName(i), cursor.getString(i));
}
list.add(map);
}
cursor.close();
return list;
}
//增删改 的方法
//返回布尔型 方便 查看 数据 操作 是否成功
public boolean executeData(String execute_content, Object[] bindArgs){
try {
if(bindArgs == null){//要绑定占位符 的参数值
db.execSQL(execute_content);
return true;
}else{
db.execSQL(execute_content, bindArgs);
return true;
}
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
=================================
//联网工具类
/**
* 联网工具类 -- 用来下载图片和文字数据
*
* @author Administrator
*
*/
public class AUtil {
public static final int TYPE_IMG = 1;
public static final int TYPE_TXT = 2;
public static Handler handler = new Handler(){};
// 线程池
public static ExecutorService executor = Executors.newFixedThreadPool(5);
// 接口
public interface Callback {
public void responseTxt(String txtData);// 对文字数据的处理
public void responseImg(String url, Bitmap bitmap);// 对图片数据的处理
public boolean isStopImg(String url);//判断下载图片是否出现 需要停止
}
/**
*
* @param type -- 下载数据的类型
* @param url -- 下载的地址
* @param callback -- 接口
*/
public static void getAsync(final int type, final String url, final Callback callback) {
executor.execute(new Runnable() {
@Override
public void run() {
try {
//联网下载数据 根据url
HttpGet get = new HttpGet(url);
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(get);
Log.i("data","===");
if(response.getStatusLine().getStatusCode() == 200){
//把下载完的数据存到一个数组
final byte[] downData = EntityUtils.toByteArray(response.getEntity());
Log.i("data", "downData.size" + downData.length);
if(callback.isStopImg(url)){//如果图片有重置情况 就终止下载
Log.i("data", "AUtil -- isStopImg");
return;
}
if(type == AUtil.TYPE_IMG){
//如果是图片就把图片保存到sdcard
ImageUtils.saveImg(url, downData);
}
//主线程 接口回调
handler.post(new Runnable() {
@Override
public void run() {
try {
if(type == AUtil.TYPE_IMG){//图片更新
//使用接口
callback.responseImg(url,
BitmapFactory.decodeByteArray(downData,
0, downData.length));
}else if(type == AUtil.TYPE_TXT){//文字更新
callback.responseTxt(new String(downData,"utf-8"));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}