zoukankan      html  css  js  c++  java
  • android保存图片到本地并可以在相册中显示出来

    app应用是越来越人性化:界面优美,服务多样化,操作还非常方便。比如我们在用app的时候,发现上面有比较的图片想保存到手机,只要点一点app上提供的保存按钮就可以了。那这个图片保存到本地怎么实现的呢?

    保存图片很简单,方法如下:

    /** 首先默认个文件保存路径 */
    private static final String SAVE_PIC_PATH=Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED) ? Environment.getExternalStorageDirectory().getAbsolutePath() : /mnt/sdcard;//保存到SD卡
    private static final String SAVE_REAL_PATH = SAVE_PIC_PATH+ /good/savePic;//保存的确切位置

    下面就是保存的方法,传入参数就可以了:

    public static void saveFile(Bitmap bm, String fileName, String path) throws IOException {
    String subForder = SAVE_REAL_PATH + path;
    File foder = new File(subForder);
    if (!foder.exists()) {
    foder.mkdirs();
    }
    File myCaptureFile = new File(subForder, fileName);
    if (!myCaptureFile.exists()) {
    myCaptureFile.createNewFile();
    }www.2cto.com
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
    bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
    bos.flush();
    bos.close();
    }

    这样就保存好了,可是有的时候明明保存下来了,为什么进入相册时查看不到呢?反正我是遇到这样的问题的,原来我们在保存成功后,还要发一个系统广播通知手机有图片更新,广播如下:

    Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    Uri uri = Uri.fromFile(file);
    intent.setData(uri);
    context.sendBroadcast(intent);//这个广播的目的就是更新图库,发了这个广播进入相册就可以找到你保存的图片了!,记得要传你更新的file哦

  • 相关阅读:
    1)BS和CS区别
    109)PHP与oracle网址
    8)对于带有 : 的语句
    将位数较多的数字看成是字符串
    7)杂项没整理
    css中的zoom的使用
    CSS布局一
    ul和ol的一些知识
    css中的display
    css中的content的使用
  • 原文地址:https://www.cnblogs.com/pbq-dream/p/5364614.html
Copyright © 2011-2022 走看看