以编程方式截取屏幕时出现内存错误

时间:2016-10-09 06:18:50

标签: android bitmap out-of-memory screenshot

我正在使用此帖How to programmatically take a screenshot in Android?中接受的答案中描述的方法截取屏幕截图。我改变该方法只是因为我没有使用该帖子中使用的public static void方法,因为我只需要将图像文件存储在存储空间中,而我不需要立即打开它。

我将此方法用作android:largeHeap="true"方法,因为我需要在我的应用的几个部分中使用它。 问题是我有内存泄漏,使用5-6次后,我得到了这种类型的错误:

  

java.lang.OutOfMemoryError:无法分配3686412字节   分配899204个空闲字节和878KB直到OOM

我尝试通过在manifest.xml.文件中设置 Canvas bitmapCanvas = new Canvas(bitmap); bitmap.eraseColor(Color.TRANSPARENT); bitmapCanvas.setBitmap(null); bitmapCanvas = null; bitmap.recycle(); 来增加可用内存的解决方案。但这只会将误差推迟2-3倍。

我还尝试首先清除位图,然后通过添加以下内容来回收它:

    outputStream.flush();
    outputStream.close();

之前和之后

public static void takeScreenshot(View v1, File f, Context context) {
    Date now = new Date();
    android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);

    try {
        String mPath = f.getPath();

        // create bitmap screen capture
        v1.setDrawingCacheEnabled(true);
        Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());

        v1.setDrawingCacheEnabled(false);

        File imageFile = new File(mPath);

        FileOutputStream outputStream = new FileOutputStream(imageFile);
        int quality = 100;
        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);


        //trying to release memory
        Canvas bitmapCanvas = new Canvas(bitmap);
        bitmap.eraseColor(Color.TRANSPARENT);
        bitmapCanvas.setBitmap(null);
        bitmapCanvas = null;
        bitmap.recycle();


        outputStream.flush();
        outputStream.close();



} catch (Throwable e) {
        // Several error may come out with file handling or OOM
        e.printStackTrace();
        Toast.makeText(context, "An error occurred!" + e.getMessage().toString() , Toast.LENGTH_LONG).show();
    }
}

但没有任何改变。有什么建议吗?

PS这是完整的方法:

Commonmethods

以下是我如何调用它(该方法放在名为 View v1 = getWindow().getDecorView().getRootView(); File imagefile = new File(Environment.getExternalStorageDirectory().getPath() + "/my_files/" + "screenshot.png"); CommonMethods.takeScreenshot(v1, imagefile, this);

的类中
response

1 个答案:

答案 0 :(得分:0)

我通过更改流的位置并添加了bitmap.recycle(),然后将其指定为null并且找不到任何内存泄漏来尝试对代码进行微小更改。

您的屏幕截图代码工作正常......

我正在附加我在android studio中尝试过的代码片段供您参考。还附上记忆日志

  

请检查捕获快照后发生的事情

package com.fun.test;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    }

    public void capture(View view) {
        View v1 = getWindow().getDecorView().getRootView();
        File imagefile = new File(Environment.getExternalStorageDirectory().getPath()+ "/screenshot.png");
        try {
            imagefile.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        takeScreenshot(v1, imagefile, this);
    }

    public void takeScreenshot(View v1, File f, Context context) {
        Date now = new Date();
        android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);

        try {
            String mPath = f.getPath();

            // create bitmap screen capture
            v1.setDrawingCacheEnabled(true);
            Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());

            v1.setDrawingCacheEnabled(false);

            File imageFile = new File(mPath);

            FileOutputStream outputStream = new FileOutputStream(imageFile);
            int quality = 100;
            bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);

            outputStream.flush();
            outputStream.close();

            //trying to release memory
            /*Canvas bitmapCanvas = new Canvas(bitmap);
            bitmap.eraseColor(Color.TRANSPARENT);
            bitmapCanvas.setBitmap(null);
            bitmapCanvas = null;*/
            bitmap.recycle();
            bitmap = null;




        } catch (Throwable e) {
            // Several error may come out with file handling or OOM
            e.printStackTrace();
            Toast.makeText(context, "An error occurred!" + e.getMessage().toString() , Toast.LENGTH_LONG).show();
        }
    }
}

这是android studio的内存分配日志

10-09 19:08:17.195 28618-28618/com.fun.test D/GestureDetector: [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 6 mFalseSizeCnt:0
10-09 19:08:17.275 28618-28618/com.fun.test D/dalvikvm: GC_FOR_ALLOC freed 245K, 23% free 7702K/9884K, paused 62ms, total 63ms
10-09 19:08:17.285 28618-28618/com.fun.test I/dalvikvm-heap: Grow heap (frag case) to 11.386MB for 2073616-byte allocation
10-09 19:08:17.295 28618-28627/com.fun.test D/dalvikvm: GC_FOR_ALLOC freed 5K, 19% free 9722K/11912K, paused 16ms, total 16ms
10-09 19:08:17.335 28618-28622/com.fun.test E/dalvikvm: adjustAdaptiveCoef max=4194304, min=1048576, ut=568
10-09 19:08:17.335 28618-28622/com.fun.test D/dalvikvm: GC_CONCURRENT freed <1K, 19% free 9722K/11912K, paused 2ms+16ms, total 38ms
10-09 19:08:17.335 28618-28618/com.fun.test D/dalvikvm: WAIT_FOR_CONCURRENT_GC blocked 27ms
10-09 19:08:17.345 28618-28618/com.fun.test I/dalvikvm-heap: Grow heap (frag case) to 13.359MB for 2073616-byte allocation
10-09 19:08:17.355 28618-28627/com.fun.test E/dalvikvm: adjustAdaptiveCoef max=6291456, min=1572864, ut=368
10-09 19:08:17.365 28618-28627/com.fun.test D/dalvikvm: GC_FOR_ALLOC freed <1K, 16% free 11747K/13940K, paused 26ms, total 26ms


10-09 19:08:37.085 28618-28618/com.fun.test D/GestureDetector: [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 3 mFalseSizeCnt:0
10-09 19:08:37.105 28618-28618/com.fun.test D/dalvikvm: GC_FOR_ALLOC freed 8126K, 23% free 7698K/9884K, paused 19ms, total 19ms
10-09 19:08:37.115 28618-28618/com.fun.test I/dalvikvm-heap: Grow heap (frag case) to 11.382MB for 2073616-byte allocation
10-09 19:08:37.125 28618-28627/com.fun.test D/dalvikvm: GC_FOR_ALLOC freed <1K, 19% free 9723K/11912K, paused 14ms, total 14ms
10-09 19:08:37.145 28618-28622/com.fun.test E/dalvikvm: adjustAdaptiveCoef max=4194304, min=1048576, ut=568
10-09 19:08:37.145 28618-28622/com.fun.test D/dalvikvm: GC_CONCURRENT freed <1K, 19% free 9723K/11912K, paused 3ms+2ms, total 20ms
10-09 19:08:37.145 28618-28618/com.fun.test D/dalvikvm: WAIT_FOR_CONCURRENT_GC blocked 10ms
10-09 19:08:37.145 28618-28618/com.fun.test I/dalvikvm-heap: Grow heap (frag case) to 13.360MB for 2073616-byte allocation
10-09 19:08:37.165 28618-28627/com.fun.test E/dalvikvm: adjustAdaptiveCoef max=6291456, min=1572864, ut=368
10-09 19:08:37.165 28618-28627/com.fun.test D/dalvikvm: GC_FOR_ALLOC freed <1K, 16% free 11748K/13940K, paused 12ms, total 12ms


10-09 19:08:51.208 28618-28618/com.fun.test D/GestureDetector: [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 3 mFalseSizeCnt:0
10-09 19:08:51.228 28618-28618/com.fun.test D/dalvikvm: GC_FOR_ALLOC freed 8124K, 23% free 7698K/9884K, paused 20ms, total 20ms
10-09 19:08:51.238 28618-28618/com.fun.test I/dalvikvm-heap: Grow heap (frag case) to 11.382MB for 2073616-byte allocation
10-09 19:08:51.248 28618-28627/com.fun.test D/dalvikvm: GC_FOR_ALLOC freed <1K, 19% free 9723K/11912K, paused 15ms, total 15ms
10-09 19:08:51.268 28618-28622/com.fun.test E/dalvikvm: adjustAdaptiveCoef max=4194304, min=1048576, ut=568
10-09 19:08:51.268 28618-28622/com.fun.test D/dalvikvm: GC_CONCURRENT freed <1K, 19% free 9723K/11912K, paused 2ms+1ms, total 19ms
10-09 19:08:51.268 28618-28618/com.fun.test D/dalvikvm: WAIT_FOR_CONCURRENT_GC blocked 11ms
10-09 19:08:51.268 28618-28618/com.fun.test I/dalvikvm-heap: Grow heap (frag case) to 13.360MB for 2073616-byte allocation
10-09 19:08:51.288 28618-28618/com.fun.test E/dalvikvm: adjustAdaptiveCoef max=6291456, min=1572864, ut=368
10-09 19:08:51.288 28618-28618/com.fun.test D/dalvikvm: GC_FOR_ALLOC freed <1K, 16% free 11748K/13940K, paused 13ms, total 13ms


10-09 19:08:58.676 28618-28618/com.fun.test D/GestureDetector: [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 4 mFalseSizeCnt:0
10-09 19:08:59.516 28618-28618/com.fun.test D/GestureDetector: [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 2 mFalseSizeCnt:0
10-09 19:08:59.546 28618-28618/com.fun.test D/dalvikvm: GC_FOR_ALLOC freed 8123K, 23% free 7698K/9884K, paused 19ms, total 20ms
10-09 19:08:59.546 28618-28618/com.fun.test I/dalvikvm-heap: Grow heap (frag case) to 11.382MB for 2073616-byte allocation
10-09 19:08:59.566 28618-28627/com.fun.test D/dalvikvm: GC_FOR_ALLOC freed <1K, 19% free 9723K/11912K, paused 15ms, total 15ms
10-09 19:08:59.586 28618-28622/com.fun.test E/dalvikvm: adjustAdaptiveCoef max=4194304, min=1048576, ut=568
10-09 19:08:59.586 28618-28622/com.fun.test D/dalvikvm: GC_CONCURRENT freed <1K, 19% free 9723K/11912K, paused 3ms+2ms, total 19ms
10-09 19:08:59.586 28618-28618/com.fun.test D/dalvikvm: WAIT_FOR_CONCURRENT_GC blocked 10ms
10-09 19:08:59.586 28618-28618/com.fun.test I/dalvikvm-heap: Grow heap (frag case) to 13.360MB for 2073616-byte allocation
10-09 19:08:59.606 28618-28627/com.fun.test E/dalvikvm: adjustAdaptiveCoef max=6291456, min=1572864, ut=368
10-09 19:08:59.606 28618-28627/com.fun.test D/dalvikvm: GC_FOR_ALLOC freed <1K, 16% free 11748K/13940K, paused 16ms, total 16ms


10-09 19:09:08.085 28618-28618/com.fun.test D/GestureDetector: [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 5 mFalseSizeCnt:0
10-09 19:09:09.066 28618-28618/com.fun.test D/GestureDetector: [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 3 mFalseSizeCnt:0
10-09 19:09:09.096 28618-28618/com.fun.test D/dalvikvm: GC_FOR_ALLOC freed 8123K, 23% free 7698K/9884K, paused 18ms, total 18ms
10-09 19:09:09.096 28618-28618/com.fun.test I/dalvikvm-heap: Grow heap (frag case) to 11.382MB for 2073616-byte allocation
10-09 19:09:09.116 28618-28627/com.fun.test D/dalvikvm: GC_FOR_ALLOC freed <1K, 19% free 9723K/11912K, paused 15ms, total 15ms
10-09 19:09:09.136 28618-28622/com.fun.test E/dalvikvm: adjustAdaptiveCoef max=4194304, min=1048576, ut=568
10-09 19:09:09.136 28618-28622/com.fun.test D/dalvikvm: GC_CONCURRENT freed <1K, 19% free 9723K/11912K, paused 3ms+1ms, total 20ms
10-09 19:09:09.136 28618-28618/com.fun.test D/dalvikvm: WAIT_FOR_CONCURRENT_GC blocked 11ms
10-09 19:09:09.136 28618-28618/com.fun.test I/dalvikvm-heap: Grow heap (frag case) to 13.360MB for 2073616-byte allocation
10-09 19:09:09.156 28618-28627/com.fun.test E/dalvikvm: adjustAdaptiveCoef max=6291456, min=1572864, ut=368
10-09 19:09:09.156 28618-28627/com.fun.test D/dalvikvm: GC_FOR_ALLOC freed <1K, 16% free 11748K/13940K, paused 15ms, total 15ms


10-09 19:09:16.163 28618-28618/com.fun.test D/GestureDetector: [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 2 mFalseSizeCnt:0
10-09 19:09:18.605 28618-28618/com.fun.test D/GestureDetector: [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 4 mFalseSizeCnt:0
10-09 19:09:18.645 28618-28618/com.fun.test D/dalvikvm: GC_FOR_ALLOC freed 8123K, 23% free 7698K/9884K, paused 31ms, total 31ms
10-09 19:09:18.645 28618-28618/com.fun.test I/dalvikvm-heap: Grow heap (frag case) to 11.383MB for 2073616-byte allocation
10-09 19:09:18.655 28618-28627/com.fun.test D/dalvikvm: GC_FOR_ALLOC freed <1K, 19% free 9723K/11912K, paused 15ms, total 15ms
10-09 19:09:18.685 28618-28622/com.fun.test E/dalvikvm: adjustAdaptiveCoef max=4194304, min=1048576, ut=568
10-09 19:09:18.685 28618-28622/com.fun.test D/dalvikvm: GC_CONCURRENT freed <1K, 19% free 9723K/11912K, paused 3ms+1ms, total 21ms
10-09 19:09:18.685 28618-28618/com.fun.test D/dalvikvm: WAIT_FOR_CONCURRENT_GC blocked 12ms
10-09 19:09:18.685 28618-28618/com.fun.test I/dalvikvm-heap: Grow heap (frag case) to 13.360MB for 2073616-byte allocation
10-09 19:09:18.695 28618-28627/com.fun.test E/dalvikvm: adjustAdaptiveCoef max=6291456, min=1572864, ut=368
10-09 19:09:18.695 28618-28627/com.fun.test D/dalvikvm: GC_FOR_ALLOC freed <1K, 16% free 11748K/13940K, paused 14ms, total 14ms
相关问题