android中如何判断sqlite数据库中的表是否为空?
private Cursor cursor;
cursor=dbManager.getDb().rawQuery("select * from photo_text;",null);
if(cursor.getCount()==0){
//内容
}
把String类型的日期串变为指定类型的Date变量,如20131005变为2013/10/06
/** * 把String类型的日期转化为Date的日期 * * @return */ @SuppressLint("SimpleDateFormat") private String[] getDate() { String[] d = new String[size]; Date date; long time; SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy/MM/dd "); for (int i = 0; i < size; i++) { try { date = sdf.parse(mDate[i]); time=(long)date.getTime(); d[i]=sdf1.format(new Date(time)); System.out.println(d[i]); } catch (ParseException e) { e.printStackTrace(); } } return d; }
加载大图,多图的方法(谷歌帮助文档里有,API guides。。。。)
package org.monsterlab.photo_text; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.util.LruCache; /** * 对图片缓存,缓存时缓存缩小的图片。 * */ public class ImageLoader { /** * 缓存核心类,用于缓存图片,在程序内存达到设定值时会将最少最近使用的图片移除掉。 */ private static LruCache<String, Bitmap> memoryCache; private static ImageLoader imageLoader; private ImageLoader() { // 获取应用程序最大可用内存 int maxMemory = (int) Runtime.getRuntime().maxMemory(); int cacheSize = maxMemory / 8; // 设置图片缓存大小为程序最大可用内存的1/8 // System.out.println("失败"); memoryCache = new LruCache<String, Bitmap>(cacheSize) { @Override protected int sizeOf(String key, Bitmap bitmap) { return bitmap.getByteCount(); } }; // System.out.println("成功"); } /** * 获取ImageCache的实例。 * * @return ImageLoader的实例。 */ public static ImageLoader getInstance() { if (imageLoader == null) { imageLoader = new ImageLoader(); } return imageLoader; } /** * 将一张图片存储到LruCache中。 * * @param key * LruCache的键,这里传入图片的URL地址。 * @param bitmap * LruCache的键,这里传入从sdcard下载的Bitmap对象。 */ public void addBitmapToMemoryCache(String key, Bitmap bitmap) { if (getBitmapFromMemoryCache(key) == null) { memoryCache.put(key, bitmap); } } /** * 从LruCache中获取一张图片,如果不存在就返回null。 * * @param key * LruCache的键,这里传入图片的URL地址。 * @return 对应传入键的Bitmap对象,或者null。 */ public Bitmap getBitmapFromMemoryCache(String key) { return memoryCache.get(key);// return a bitmap } /** * 计算图片大小 * @param options * Bitmap对象 * @param reqWidth * 要求的宽度 * @param reqHeight * 要求的高度 * @return */ public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { // 源图片的高度和宽度 final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { // 计算出实际宽高和目标宽高的比率 final int heightRatio = Math.round((float) height / (float) reqHeight); final int widthRatio = Math.round((float) width / (float) reqWidth); // 选择宽和高中最小的比率作为inSampleSize的值,这样可以保证最终图片的宽和高 // 一定都会大于等于目标的宽和高。 inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; } return inSampleSize; } /** * 解析源图片 * @param resources * * @param pathName * 对文件进行解码的完整路径名称。 * @param reqWidth * 需求宽度 * @param i * @return */ public static Bitmap decodeSampledBitmapFromResource(String pathName, int reqHeight,int reqWidth) { // 第一次解析将inJustDecodeBounds设置为true,来获取图片大小 final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(pathName, options); // 调用上面定义的方法计算inSampleSize值 options.inSampleSize = calculateInSampleSize(options, reqHeight ,reqWidth); // 使用获取到的inSampleSize值再次解析图片 options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(pathName, options); } }