MediaScannerConnection生成android.app.ServiceConnectionLeaked
我正在使用的MediaScannerConnection示例代码来自 我使用的代码片段是:MediaScannerConnection生成android.app.ServiceConnectionLeaked,android,sample,Android,Sample,我正在使用的MediaScannerConnection示例代码来自 我使用的代码片段是: MediaScannerConnection.scanFile( context, new String[] { permFile.getAbsolutePath() }, null, new MediaScannerConnection.OnScanCompletedListener() { public void onScanCompleted(Str
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);
}
});
当我运行此代码时,我从以下位置获得一个FC对话框:
我做错了什么
仅供参考,我正在使用AsyncTask从后台线程运行此程序。当我更改布局等时,语音识别器出现问题 我所要做的就是添加一个未注册的接收者,就像onActivityResult中的那样:
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
unregisterReceiver(mReceiver);
super.onDestroy();
}
我使用的文档中提供的代码片段注意到了类似的错误消息 该代码正常工作,并在设备库中显示一个新文件,但同时打印有关
泄漏服务连接的错误
查看MediaScannerConnection
的内部Android代码,似乎存在某种机制在最后一个文件之后停止服务。如果只给一个文件,它可能不起作用
我最终使用了一个完全不同的解决方案,通过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
这似乎是首选的方式,在中也提到了。使用
getApplicationContext()
。我感觉MediaScannerConnection正在泄漏侦听器。不知何故,它没有得到清理,也没有方法重置侦听器。我现在面临着同样的问题。你同时解决了这个问题吗?我不记得了。这是我一年前贴的。我得回去看看。我还在KitKat上看到这个。很确定这是安卓系统的问题,不是使用问题。回答不错。当使用IntentService
时,这消除了泄漏。在非故意服务中也非常有效!
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