调用MediaScannerConnection 发生内存泄露的解决方法
调用MediaScannerConnection发起扫描时经常会发生内存泄露,例如:
E ActivityThread: Activity FolderListActivity has leaked ServiceConnection android.media.MediaScannerConnection@ec2a697 that was originally bound here
从网上看到一种解决方法,就是把MediaScannerConnection单独放在一个类中,而不是直接在Activity中创建。
参考网址:
http://www.dreamincode.net/forums/topic/289977-service-connection-leak-error/
1.有问题的代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.createfile);
setupVariables();
checkState();
//Locates the SD Card's path to create a new directory
//If the directory does not exist, then it is created
path = new File(Environment.getExternalStorageDirectory(), dirName);
if(!path.exists()){
path.mkdirs();
}
//if you can read and write to storage
if (wEnable && rEnable) {
// Can Read and Write
confirm.setonclickListener(this);
// creates the library file
save.setonclickListener(this);
}
}
private void setupVariables() {
//The Variables are stored here
}
private void checkState() {
//Checks the state of the external storage. Not relevant to the problem
}
public void onclick(View v) {
switch (v.getId()) {
case R.id.bConfirm:
name = tv.getText().toString();
if (name.equals("")) {
error.setVisibility(View.VISIBLE);
}
else {
error.setVisibility(View.INVISIBLE);
save.setVisibility(View.VISIBLE);
}
break;
case R.id.bSave:
name = tv.getText().toString() + ".txt";
library = new File(path, name);
//This is where the problem occurs
MediaScannerConnection.scanFile(FileCreation.this, new String[]{library.toString()},
null, new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
// TODO Auto-generated method stub
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(FileCreation.this,
"Media scan completed",
Toast.LENGTH_SHORT).show();
}
});
}
});
finish();
break;
}
}
2.建议的代码:
You are creating a media scanner and running it and then calling finish(); while the service is still connected!
Create the following class :
import java.io.File;
import android.content.Context;
import android.media.MediaScannerConnection;
import android.media.MediaScannerConnection.MediaScannerConnectionClient;
import android.net.Uri; public class SingleMediaScanner implements MediaScannerConnectionClient { public interface ScanListener{
public void onScanFinish();
} private MediaScannerConnection mMs;
private File mFile;
private ScanListener listener; public SingleMediaScanner(Context context, File f,ScanListener l) {
listener = l;
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();
listener.onScanFinish();
} }
Then in your code do the following:
new SingleMediaScanner(<context>,<File go here>,new ScanListener(){
public void onScanFinish()
{
finish();
Toast.makeText(FileCreation.this,"Media scan completed",Toast.LENGTH_SHORT).show();
});
3.修改后的代码:
package com.maclinCode; import java.io.File; import android.app.Activity;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.onclickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast; import com.maclinCode.SingleMediaScanner.ScanListener; public class FileCreation extends Activity implements onclickListener {
EditText tv;
TextView error;
Button save, confirm;
boolean wEnable, rEnable;
String name; File path = null;
File fileName = null;
File library = null;
private String state;
private static final String dirName = "GameLibrary";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.createfile);
setupVariables();
checkState(); //Locates the SD Card's path to create a new directory
//If the directory does not exist, then it is created
path = new File(Environment.getExternalStorageDirectory(), dirName);
if(!path.exists()){
path.mkdirs();
} //if you can read and write to storage
if (wEnable && rEnable) {
// Can Read and Write
confirm.setonclickListener(this);
// creates the library file
save.setonclickListener(this);
}
} private void setupVariables() {
confirm = (Button) findViewById(R.id.bConfirm);
save = (Button) findViewById(R.id.bSave);
tv = (EditText) findViewById(R.id.FileName); error = (TextView) findViewById(R.id.tvError);
state = Environment.getExternalStorageState();
wEnable = rEnable = false; } private void checkState() {
if (state.equals(Environment.MEDIA_MOUNTED)) {
// Can Read and Write to storage
wEnable = rEnable = true;
}
else{
Toast.makeText(this, "Cannot use External Storage", Toast.LENGTH_SHORT).show();
finish();
return;
}
} public void onclick(View v) {
switch (v.getId()) {
case R.id.bConfirm:
name = tv.getText().toString(); if (name.equals("")) {
error.setVisibility(View.VISIBLE);
}
else {
error.setVisibility(View.INVISIBLE);
save.setVisibility(View.VISIBLE);
}
break; case R.id.bSave:
name = tv.getText().toString() + ".txt";
library = new File(path, name); // MediaScannerConnection.scanFile(FileCreation.this, new String[]{library.toString()},
// null, new MediaScannerConnection.OnScanCompletedListener() {
// public void onScanCompleted(String path, Uri uri) {
// // TODO Auto-generated method stub
// runOnUiThread(new Runnable() {
// public void run() {
// Toast.makeText(FileCreation.this,
// "Media scan completed",
// Toast.LENGTH_SHORT).show();
// }
// });
// }
// });
// finish();
new SingleMediaScanner(FileCreation.this, library, new ScanListener(){
public void onScanFinish(){
finish();
Toast.makeText(FileCreation.this,
"Media scan completed",
Toast.LENGTH_SHORT).show();
}
});
break;
} } }
调用MediaScannerConnection 发生内存泄露的解决方法的更多相关文章
- 分享.net常见的内存泄露及解决方法
分享.net常见的内存泄露及解决方法 关于内存泄漏的问题,之前也为大家介绍过,比如:<C++中内存泄漏的检测方法介绍>,是关于C++内存泄漏的.今天为大家介绍的是关于.NET内存泄漏的问题 ...
- JS高程中的垃圾回收机制与常见内存泄露的解决方法
起因是因为想了解闭包的内存泄露机制,然后想起<js高级程序设计>中有关于垃圾回收机制的解析,之前没有很懂,过一年回头再看就懂了,写篇博客与大家分享一下. #内存的生命周期: 分配你所需要的 ...
- Android APP常见的5类内存泄露及解决方法
1.static变量引起的内存泄漏 因为static变量的生命周期是在类加载时开始 类卸载时结束,也就是说static变量是在程序进程死亡时才释放,如果在static变量中 引用了Activity 那 ...
- WPF不明内存泄露已解决,白头发也没了
原文:WPF不明内存泄露已解决,白头发也没了 在使用OpenExpressApp进行WPF应用开发过程中遇到多个内存泄漏的地方,在上一篇中求助了一个内存泄露问题[WPF不明内存泄露原因,头发都白了几根 ...
- Java常见的几种内存溢出及解决方法
Java常见的几种内存溢出及解决方法[情况一]:java.lang.OutOfMemoryError:Javaheapspace:这种是java堆内存不够,一个原因是真不够(如递归的层数太多等),另一 ...
- ASP.NET MVC 3 loginUrl自动变成Account/Login,并且发生404错误的解决方法
http://www.cnblogs.com/think8848/archive/2011/07/08/2100814.html ASP.NET MVC 3 loginUrl自动变成Account/L ...
- 《对“XXX::Invoke”类型的已垃圾回收委托进行了回调。这可能会导致应用程序崩溃、损坏和数据丢失。向非托管代码传递委托时,托管应用程序必须让这些委托保持活动状态,直到确信不会再次调用它们》的问题的解决方法
<对“XXX::Invoke”类型的已垃圾回收委托进行了回调.这可能会导致应用程序崩溃.损坏和数据丢失.向非托管代码传递委托时,托管应用程序必须让这些委托保持活动状态,直到确信不会再次调用它们& ...
- 【javascript】内存泄露及其解决办法
1.内存泄露:一般由于开发者使用不当导致不用的内存没有被操作系统或者空闲内存池回收释放. 2.造成内存泄露的常见原因: 1) 意外的全局变量引起的内存泄露 2)闭包引起的内存泄露 闭包可以维持函数内局 ...
- PF不明内存泄露已解决,白头发也没了(转)
在使用OpenExpressApp进行WPF应用开发过程中遇到多个内存泄漏的地方,在上一篇中求助了一个内存泄露问题[WPF不明内存泄露原因,头发都白了几根],本篇与大家分享一下如何解决此问题的过程. ...
随机推荐
- hdu 2865 Polya计数+(矩阵 or 找规律 求C)
Birthday Toy Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- [Codeforces]663E Binary Table
某变换好题.不过听说还有O(2^n*n^2)DP的…… Description 给定一个n*m的01矩阵,你可以选择对任意行和任意列取反,使得最终“1”的数量尽量少. Input 第一行两个整数n,m ...
- bzoj1934
1934: [Shoi2007]Vote 善意的投票 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 2406 Solved: 1498[Submit][ ...
- 常用Markdown语法
个人常用的Markdown语法 前言 初次使用Markdown编辑器,将自己最常用的几个语法记录一下,如有错误或是更方便的使用方式还请指出. 多级标题 一个"#"到六个" ...
- break用法
break用于循环,则是终止循环 break用于switch,则终止switch break不能用于if.当break用于if语句,但是终止的是if的外部循环 break 只能终止最近的循环
- VC++6.0的使用方法
VC++6.0的最基本使用方法,创建一个c++项目工程可参考:https://jingyan.baidu.com/article/8ebacdf0cbdb5749f75cd54a.html 这里面的操 ...
- 【docker简易笔记】docker基础信息的分享
docker 使用的频率越来越高,所以在后续的一些博客中会分享一些docker的安装和使用. 一.docker介绍 "Docker 最初是 dotCloud 公司创始人 Solomon ...
- 关于return的一些了解
写return是一种清晰的风格,可以防止一些意外的错误. 所以书上只说应该写,而不是必须写. 如果符合某个条件要退出的话,可以用return返回,否则可以不写这句代码的,当程序执行到"}&q ...
- textarea不能使用maxlength
知道文本框有个maxlength属性,有次开发项目中使用了textarea标签,没去看文档,直接加了maxlength属性,且有效果没有报错,喜滋滋的用了,结果没两天就测试出了bug 问题描述:文本域 ...
- python的模块与包的导入
类似于C语言的包含头文件去引用其他文件的函数,python也有类似的机制,常用的引入方法有以下 import 模块名 #模块名就是py文件名 #使用这种方法以后调用函数的时候要使用模块名.函数名()这 ...