我正在构建一个应用程序,它从服务器下载 2 张图像,将它们保存到 SD 卡并显示它们(循环,每 15 秒更改一次图像)。
我完成了,但几分钟后程序崩溃:OutOfMemoryError:位图超出 VM 预算的大小。
因此,某处一定存在内存泄漏。我对 android 编程还是很陌生:不太确定我需要做哪些 VM 不会做的清理工作。
有什么帮助吗?
private ImageView mImageView;
private Bitmap mImageBitmap;
public static String rslt="";
/** Called when the activity is first created. */
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
mImageView = (ImageView) findViewById(R.id.imageView1);
mImageBitmap=null;
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
int count = 1;
public void run() {
Bitmap bm = getNewImages();
if (bm != null){
mImageView.setImageBitmap(bm);
bm=null;
System.gc();
handler.postDelayed(this, 15* 1000);
}else if(count==1){
File img1 = new File(Environment.getExternalStorageDirectory(), "1.jpg");
if(img1.exists()){
bm = BitmapFactory.decodeFile(img1.getAbsolutePath());
if (bm != null){
mImageView.setImageBitmap(bm);
bm=null;
}
}
count = 2;
System.gc();
handler.postDelayed(this, 15* 1000);
}else if (count==2){
File img2 = new File(Environment.getExternalStorageDirectory(), "2.jpg");
if(img2.exists()){
bm = BitmapFactory.decodeFile(img2.getAbsolutePath());
if (bm != null){
mImageView.setImageBitmap(bm);
bm=null;
}
}
count=1;
System.gc();
handler.postDelayed(this, 15* 1000);
}
}
private Bitmap getNewImages() {
ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
mImageBitmap=null;
if (mWifi.isConnected()) {
try{
// EditText ed1=(EditText)findViewById(R.id.editText1);
// String rego=ed1.getText().toString();
String rego="AGR905";
rslt="START";
Caller c=new Caller();
c.rego=rego;
c.join();
c.start();
while(rslt=="START")
{
try
{
Thread.sleep(10);
}
catch(Exception ex)
{
}
}
}
catch(Exception ex){
}
if (rslt.length()<=1600){
}else{
byte[] decodedString = Base64.decode(rslt, Base64.DEFAULT);
mImageBitmap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
mImageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File from = new File(Environment.getExternalStorageDirectory(), "1.jpg");
if(from.exists()){
File to = new File(Environment.getExternalStorageDirectory(), "2.jpg");
from.renameTo(to);
from.delete();
}
//you can create a new file name "test.jpg" in sdcard folder.
File f = new File(Environment.getExternalStorageDirectory()+ File.separator + "1.jpg");
try {
f.createNewFile();
//write the bytes in file
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
// remember close de FileOutput
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return mImageBitmap;
}
};
handler.postDelayed(runnable, 2000); //for initial delay..