Android中的内存跟踪/泄漏

时间:2011-08-10 05:22:17

标签: android memory-management ddms

每次运行此块时出现一些内存问题,DDMS中使用的内存%每次上升2%,直到它最终在位图分配上崩溃。

    Uri U = null;

    try {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            // We can read and write the media
            File path = this.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
            File file = new File(path, "image.jpg");
            path.mkdirs();

            FileOutputStream Os = new FileOutputStream(file);
            getFullResFilteredImage().compress(Bitmap.CompressFormat.JPEG, 80, Os);

            Os.flush();
            Os.close();
            Os = null;

            U = Uri.fromFile(file);
            file.deleteOnExit();
            file = null;
        } else {
            Toast toast = Toast.makeText(PrintLabActivity.this.getApplicationContext(), 
                    PrintLabActivity.this.getResources().getString(R.string.sd_image_error), Toast.LENGTH_SHORT);
            toast.show();
            return;
        }

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    Intent i = new Intent(Intent.ACTION_SEND);
    i.setType("image/jpg");
    i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
    i.putExtra(Intent.EXTRA_STREAM, U);

    i.putExtra(Intent.EXTRA_SUBJECT, this.getResources().getString(R.string.email_subject));
    String message = this.getResources().getString(R.string.email_message, PNBApplication.MARKETPLACE_URL, PNBApplication.MARKETPLACE_URL);
    i.putExtra(Intent.EXTRA_TEXT   , Html.fromHtml(message));
    U = null;

    this.startActivity(Intent.createChooser(i,"Send Image To:"));

如果此代码中没有任何内容可以泄漏,那么有没有办法告诉每次分配的内容?

1 个答案:

答案 0 :(得分:0)

创建位图时,可以使用BitmapFactory.Options.inSampleSize减少内存消耗。

如需更详细的解答,请发布你的getFullResFilteredImage()方法。

相关问题