Android入门——数据存储之IO文件流操作小结

来源:互联网 发布:win8数据恢复软件 编辑:程序博客网 时间:2024/05/26 02:19

引言

Android数据存储基本可以分为四种:数据库(SQLite、其他远程网络服务器)、轻量级的本地存储SharedPreference、内容提供器ContentProvider数据共享、文件File IO流。前期的文章总结了前面三种的基本语法,这一篇迟来的文件IO流的总结,主要就是一些常用方法的总结。

一、Bitmap与InputStream的转化

 /** * 位图转为InputSteam * @param bitmap * @return */public static InputStream bitmap2InputStream(Bitmap bitmap) {    ByteArrayOutputStream byteArrOutStream = new ByteArrayOutputStream();    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrOutStream);    InputStream inStream = new ByteArrayInputStream(byteArrOutStream.toByteArray());    return inStream;}// 将InputStream转换成Bitmappublic static Bitmap inputStream2Bitmap(InputStream inStream) {    //return BitmapFactory.decodeStream(inStream);     BitmapFactory.Options options=new BitmapFactory.Options();     options.inJustDecodeBounds = false;     options.inSampleSize = 10;   //width,hight都设为原来的十分一,If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory.     Bitmap btp =BitmapFactory.decodeStream(inStream,null,options);     return btp;}

二、Bitmap与Drawable的转化

/*** Drawable转换成Bitmap*/public static Bitmap drawable2Bitmap(Drawable drawable) {   Bitmap bitmap = Bitmap           .createBitmap(                   drawable.getIntrinsicWidth(),                   drawable.getIntrinsicHeight(),                   drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888                           : Bitmap.Config.RGB_565);   Canvas canvas = new Canvas(bitmap);   drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),           drawable.getIntrinsicHeight());   drawable.draw(canvas);   return bitmap;}// Bitmap转换成Drawablepublic static Drawable bitmap2Drawable(Bitmap bitmap) {   BitmapDrawable bd = new BitmapDrawable(bitmap);   Drawable d = (Drawable) bd;   return d;}

四、Bitmap转为byte[]

/***  Bitmap转成byte[]*/public static byte[] bitmap2Bytes(Bitmap bitmap) {    ByteArrayOutputStream byteArrOutStream = new ByteArrayOutputStream();    bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrOutStream);    return byteArrOutStream.toByteArray();}/** *  byte[]转换成Bitmap   */public static Bitmap bytes2Bitmap(byte[] bytes) {    if (bytes.length != 0) {        return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);    }    return null;}

五、以最省内存的方式和最大程度避免OOM读取本地资源图片

/*** 以最省内存的方式读取本地资源的图片* @param context* @param resId* @return*/public static Bitmap readBitMap(@NonNul Context context, @NonNul int resId){   BitmapFactory.Options opt = new BitmapFactory.Options();   opt.inPreferredConfig = Bitmap.Config.RGB_565;   opt.inInputShareable = true;   //获取资源图片   InputStream is = context.getResources().openRawResource(resId);   return BitmapFactory.decodeStream(is,null,opt);}

六、保存输入流到指定路径

/**     *     * @param inStream  文件的输入流     * @param savePath  保存该文件的路径,包括所有父目录     * @param fileName  文件的名称     * @return     */    public static boolean saveFile(InputStream inStream, String savePath, String fileName) {        boolean result = false;        //File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),"FKEZOpenSDK/CapturePicture/20161028/1522483305.jpg");        File file=new File(savePath,fileName);        FileOutputStream fileOutputStream;//存入SDCard的流        ByteArrayOutputStream outStream;        try {            //首先判断文件是否存在            if(!file.exists()){                //再判断文件的父目录是否存在                if(file.getParentFile() != null){                    file.getParentFile().mkdirs();//父目录不存在则创建                }                file.createNewFile();//创建文件            }            //把流写入到文件中            fileOutputStream = new FileOutputStream(file);            byte[] buffer = new byte[10];            outStream = new ByteArrayOutputStream();            int len;            while((len = inStream.read(buffer)) != -1) {                outStream.write(buffer, 0, len);            }            byte[] bs = outStream.toByteArray();            fileOutputStream.write(bs);            fileOutputStream.flush();            fileOutputStream.close();            result = true;        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }finally {        }        return result;    }

七、获取当前路径下的可用空间

/** * 获取当前路径剩余空间的大小以字节为单位 * @return */public static long  getSDCardRemainSize(String path){    //StatFs statfs = new StatFs(Environment.getExternalStorageDirectory().getPath());    StatFs statfs = new StatFs(path);    long blockSize = statfs.getBlockSizeLong();    long availableBlocks = statfs.getAvailableBlocksLong();    return availableBlocks * blockSize;}

八、获取SDCard的路径

/**     * 获取SDCard的保存路径     * @return     */    public static String  getSDCardPath(){        return Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED) ? Environment.getExternalStorageDirectory().getAbsolutePath() : "/mnt/sdcard";    }

待续…

0 0