Android 图片在SD卡及包下的存储

public class FileBitmap {
    /**
     * 获取sd卡中的bitmap,bitmap可见
     * 
     * @param bitmap
     *            读取bitmap的路径
     * @return bitmap
     */
    public static Bitmap getBitmapByPath(String fileNameString, String bitmapURL) {
        String bitmapName = bitmapURL.substring(bitmapURL.lastIndexOf("/") + 1);
        fileNameString = fileNameString + "/" + bitmapName;
        BitmapFactory.Options options = new BitmapFactory.Options();
        Bitmap bm = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+fileNameString, options);
        return bm;
    }

    /**
     * 在SD卡中存储bitmap,bitmap可见
     * 
     * @param fileName
     *            保存bitmap的文件夹路径
     * @param bitName
     *            bitmap的路径
     * @param mBitmap
     *            要保存的bitmap
     * @throws IOException
     */
    public static void saveMyBitmap(String fileName, String bitmapURL,
            Bitmap mBitmap) throws IOException {
        String bitmapName = bitmapURL.substring(bitmapURL.lastIndexOf("/") + 1); // 传入一个远程图片的url,然后取最后的图片名字
        File tmp = new File(Environment.getExternalStorageDirectory()+fileName);
        
        if (!tmp.exists()) {
            tmp.mkdir();
        }
        File f = new File(Environment.getExternalStorageDirectory()+fileName+"/"+bitmapURL);
        f.createNewFile();
        FileOutputStream fOut = null;
        try {
            fOut = new FileOutputStream(f);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
        
        try {
            fOut.flush();
            fOut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    /*
     * 保存图片到本地,这个是把图片压缩成字节流然后保存到本地,所以本地的图片是无法显示的
     * 
     * @param mBitmap
     * 
     * @param imageURL
     * 
     * @param cxt
     */
    public static void saveBitmap(Bitmap mBitmap, String imageURL, Context cxt) {

        String bitmapName = imageURL.substring(imageURL.lastIndexOf("/") + 1); // 传入一个远程图片的url,然后取最后的图片名字

        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        mBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] byteArray = stream.toByteArray();

        FileOutputStream fos = null;
        ObjectOutputStream oos = null;

        try {
            fos = cxt.openFileOutput(bitmapName, Context.MODE_PRIVATE);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(byteArray);
        } catch (Exception e) {
            e.printStackTrace();
            // 这里是保存文件产生异常
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    // fos流关闭异常
                    e.printStackTrace();
                }
            }
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    // oos流关闭异常
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 读取本地私有文件夹的图片
     * 
     * @param name
     * @param cxt
     * @return
     */
    public static Bitmap getBitmap(String fileName, Context cxt) {
        String bitmapName = fileName.substring(fileName.lastIndexOf("/") + 1);
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        try {
            fis = cxt.openFileInput(bitmapName);
            ois = new ObjectInputStream(fis);
            byte[] byteArray = (byte[]) ois.readObject();
            Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0,
                    byteArray.length);
            return bitmap;
        } catch (Exception e) {
            e.printStackTrace();
            // 这里是读取文件产生异常
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    // fis流关闭异常
                    e.printStackTrace();
                }
            }
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    // ois流关闭异常
                    e.printStackTrace();
                }
            }
        }
        // 读取产生异常,返回null
        return null;
    }

}

 

posted @ 2015-07-01 16:57  Pepper.B  阅读(713)  评论(0编辑  收藏  举报