我使用的是API Demos中的MediaScannerConnection示例代码
我使用的代码片段是:
MediaScannerConnection.scanFile(
context,
new String[] { permFile.getAbsolutePath() },
null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
android.util.Log.i("ExternalStorage", "Scanned " + path + ":");
android.util.Log.i("ExternalStorage", "-> uri=" + uri);
}
});当我运行这段代码时,我从LogCat获得了一个包含以下内容的FC对话框
4-20 23:17:45.988: ERROR/ActivityThread(3015): Activity com.my.package.name has leaked ServiceConnection android.media.MediaScannerConnection@40715c70 that was originally bound here
04-20 23:17:45.988: ERROR/ActivityThread(3015): android.app.ServiceConnectionLeaked: Activity com.my.package.name has leaked ServiceConnection android.media.MediaScannerConnection@40715c70 that was originally bound here我做错了什么?
仅供参考,我使用AsyncTask从后台线程运行此程序。
发布于 2012-10-10 22:32:43
使用Environment.getExternalStoragePublicDirectory文档中提供的代码片段,我注意到了类似的错误消息。
代码按照预期运行良好,并在设备库中显示一个新文件,但同时打印有关leaked ServiceConnection的错误。
查看MediaScannerConnection的内部安卓代码,似乎存在某种机制,可以在最后一个文件后停止服务。当只给出一个文件时,它可能不起作用?
我最终使用了一个完全不同的解决方案,通过Intent通知MediaScanner。这也运行得很好,并且不会产生任何警告:
Intent mediaScannerIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri fileContentUri = Uri.fromFile(permFile); // With 'permFile' being the File object
mediaScannerIntent.setData(fileContentUri);
this.sendBroadcast(mediaScannerIntent); // With 'this' being the context, e.g. the activity这似乎是首选的方式,而且在Android Training about Taking Photos中也提到了。
发布于 2014-08-20 20:33:11
请改用getApplicationContext()。
发布于 2012-10-08 10:07:10
当我要更改布局时,我的语音识别器就遇到了这个问题。
我所要做的就是在onActivityResult中添加一个类似如下的unregisterReceiver:
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
unregisterReceiver(mReceiver);
super.onDestroy();
}https://stackoverflow.com/questions/5739140
复制相似问题