zoukankan      html  css  js  c++  java
  • 主动通知Android系统图库进行更新

    项目中遇到调用图库进行图片的选择,因为不能主动及时更新,遂实现代码调用实现主动及时更新。

    废话不多刷,看代码。

    方式一,发送一个广播,

    sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,Uri.parse("file://"+fileSD_file)));

    方式二,通过MediaScannerConnection 类

    MediaScannerConnection.scanFile(context, new String[]{fileSD_file.toString()}, null, null);

    方式三,也是通过MediaScannerConnection 类

    MediaScannerConnection msc=new MediaScannerConnection(context,new MediaScannerConnectionClient(){
    @Override
    public void onMediaScannerConnected() {
    // TODO Auto-generated method stub

    }
    @Override
    public void onScanCompleted(String path, Uri uri) {
    // TODO Auto-generated method stub

    }
    }); 
    msc.connect();
    try {
    Thread.sleep(1000);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    URL url = null;
    try {
    url = fileSD_file.toURL();
    } catch (MalformedURLException e) {
    e.printStackTrace();
    }

    MimeTypeMap mtm=MimeTypeMap.getSingleton();

    msc.scanFile(fileSD_file.toString(), mtm.getMimeTypeFromExtension(mtm.getFileExtensionFromUrl(url.toString())));

    //此句上面的一句可以,下面的一句也可以,都适合这种方法(已用颜色标示)。

    // msc.scanFile(fileSD_file.getAbsolutePath(), null);

    msc.disconnect();

    来源:http://www.bozhiyue.com/anroid/boke/2016/0318/3524.html

  • 相关阅读:
    linux 笔试题
    shell -Z- d等等代表
    shell中for循环总结
    linux启动过程
    linux面试题3
    linux面试题2
    小峰servlet/jsp(4)EL表达式
    小峰servlet/jsp(3)登陆功能实现
    小峰servlet/jsp(2)
    java日期比较例子等...
  • 原文地址:https://www.cnblogs.com/ruiati/p/4061979.html
Copyright © 2011-2022 走看看