我刚发现
ACTION_MEDIA_REMOVED
我正在寻找类似的东西:
ACTION_MEDIA_INSERTED和ACTION_MEDIA_CHANGED
您可以使用FileObserver
来观察文件或目录中的任何更改。用法示例。
在清单文件中添加以下权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
public class MyFileObserver extends FileObserver {
String absolutePath = "path to your directory";
public MyFileObserver(String path) {
super(path, FileObserver.CREATE);
absolutePath = path;
}
@Override
public void onEvent(int event, String path) {
// this is where you will receive all event like file modified, file added...
if (path != null) {
Log.e("File created..");
}
}
}
在你的活动中
public class MainActivity extends AppCompatActivity {
MyFileObserver myFileObserver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myFileObserver = new DirectoryFileObserver("Path of the directory");
myFileObserver.startWatching();
}
}
如果你想在后台观察外部存储,那么请参考这个answer