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;    }}

更多相关文章

  1. android 加载sd卡的图片
  2. android launcher壁纸滚动图片拉伸问题
  3. Android中调用相册里面的图片并返回:
  4. Android学习笔记(二)android studio基本控件及布局(实现图片查看器)
  5. Android更换RatingBar图片
  6. Android (滑动屏幕切换图片的实现)
  7. android中实现背景图片颜色渐变方法
  8. android缩放图片
  9. Android 实现局部图片滑动指引效果

随机推荐

  1. 第三十六天 一乐在其中—Android的按钮单
  2. 适用于 Android 8.0 及以下全部版本的拍
  3. 随笔7
  4. Android Render(一)Activity窗口构成和绘
  5. android的Splash Screen
  6. Android中的Intent在两个页面传值问题
  7. Android 自定义PopupWindow实现悬浮窗效
  8. Android 移动开发一本就够学习笔记一
  9. MPAndroidChar--饼状图
  10. 搭建WebRTC Android视频demo