MediaScannerConnection产生android.app.ServiceConnectionLeaked [英] MediaScannerConnection produces android.app.ServiceConnectionLeaked

查看:1886
本文介绍了MediaScannerConnection产生android.app.ServiceConnectionLeaked的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是从<一个的MediaScannerConnection例如code href="http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.html">API演示

我使用的片段是:

    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);
        }
    });

当我运行此code,我得到了FC对话框从LogCat中以下内容:

When I run this code I get a FC dialog with the following from the LogCat:

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的运行这一点。

What am I doing wrong? FYI I'm running this from a background thread using AsyncTask.

推荐答案

我使用具备的<一个文档中的code段发现同样的错误讯息href="http://developer.android.com/reference/android/os/Environment.html#getExternalStoragePublicDirectory%28java.lang.String%29">Environment.getExternalStoragePublicDirectory.

在code正常工作,如预期使得新文件中的设备库可见,但在同一时间打印关于泄露ServiceConnection

The code works fine as expected and makes a new file visible in the device gallery, but at the same time prints the error about the leaked ServiceConnection.

综观内部的Andr​​oid code中的 MediaScannerConnection 好像某种机制存在的最后一个文件后,停止该服务。也许只给出了一个文件时不工作?

Looking at the internal android code of the MediaScannerConnection it seems some kind of mechanism exists to stop the service after the last file. Maybe it doesn't work when given only one file?

我最终被通过意向通知MediaScanner使用整个不同的解决方案。这工作正常,也并没有产生任何警告:

I ended up to use an entire different solution by informing the MediaScanner via Intent. This works fine too and in is not producing any warnings:

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

看来这是preferred的方式,因为它被提及的Andr​​oid培训约以照片了。

这篇关于MediaScannerConnection产生android.app.ServiceConnectionLeaked的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆