gpt4 book ai didi

android - MediaScanner ServiceConnectionLeaked

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:13:57 26 4
gpt4 key购买 nike

我使用这个类来扫描我在 SDCard 中的应用图像。

public class SingleMediaScanner implements MediaScannerConnectionClient {

private MediaScannerConnection mMs;
private File mFile;

public SingleMediaScanner(Context context, File f) {
mFile = f;
mMs = new MediaScannerConnection(context, this);
mMs.connect();
}

@Override
public void onMediaScannerConnected() {
mMs.scanFile(mFile.getAbsolutePath(), null);
}

@Override
public void onScanCompleted(String path, Uri uri) {
mMs.disconnect();
}
}

它有效,但在我的 LogCat 中它总是显示错误:

12-29 16:44:16.022: ERROR/ActivityThread(21807): Activity com.cny.ecard.CustomListDialog has leaked ServiceConnection android.media.MediaScannerConnection@450fb8e0 that was originally bound here
12-29 16:44:16.022: ERROR/ActivityThread(21807): android.app.ServiceConnectionLeaked: Activity com.cny.ecard.CustomListDialog has leaked ServiceConnection android.media.MediaScannerConnection@450fb8e0 that was originally bound here
12-29 16:44:16.022: ERROR/ActivityThread(21807): at android.app.ActivityThread$PackageInfo$ServiceDispatcher.<init>(ActivityThread.java:1121)
12-29 16:44:16.022: ERROR/ActivityThread(21807): at android.app.ActivityThread$PackageInfo.getServiceDispatcher(ActivityThread.java:1016)
12-29 16:44:16.022: ERROR/ActivityThread(21807): at android.app.ContextImpl.bindService(ContextImpl.java:863)
12-29 16:44:16.022: ERROR/ActivityThread(21807): at android.content.ContextWrapper.bindService(ContextWrapper.java:347)
12-29 16:44:16.022: ERROR/ActivityThread(21807): at android.media.MediaScannerConnection.connect(MediaScannerConnection.java:117)
12-29 16:44:16.022: ERROR/ActivityThread(21807): at com.cny.ecard.SingleMediaScanner.<init>(SingleMediaScanner.java:19)

问题是什么?谢谢。

最佳答案

对于扫描单个文件,或从无法绑定(bind)到服务的上下文(例如,从 BroadcastReceiver)扫描文件,user430926 的评论值得单独回答:

Uri contentUri = Uri.fromFile(file);
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(contentUri);
sendBroadcast(mediaScanIntent);

关于android - MediaScanner ServiceConnectionLeaked,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8665641/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com