主動通知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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM