Monodroid Take Screenshots内存不足

时间:2012-09-24 20:30:48

标签: android bitmap xamarin.android screenshot

所以我正在尝试创建一个功能,它将基本上截取当前应用活动的屏幕截图并在共享之前将其显示在屏幕上,但是当它到达我用于显示的屏幕时内存耗尽截至分享之前的屏幕截图。

// image naming and path  to include sd card  appending name you choose for file
Java.IO.File dir = new Java.IO.File(Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures), "ScreenShots");

if (!dir.Exists())
    dir.Mkdirs();

// create bitmap screen capture
Bitmap bitmap;
View v1 = act.RootView;
v1.DrawingCacheEnabled = true;
bitmap = Bitmap.CreateBitmap(v1.DrawingCache);
v1.DrawingCacheEnabled = false;

string date = DateTime.Now.ToString();

date = date.Replace('/', '-');

Java.IO.File _file = new Java.IO.File(dir, from + date + ".png");
try
{
    using (var stream = System.IO.File.Create(_file.AbsolutePath))
    {
        bitmap.Compress(Android.Graphics.Bitmap.CompressFormat.Png, 100, stream);
        stream.Flush();
        stream.Close();
    }
    bitmap.Dispose();
    v1.Dispose();
}
catch (Java.IO.FileNotFoundException e)
{
    // TODO Auto-generated catch block
    e.PrintStackTrace();
}
catch (Java.IO.IOException e)
{
    // TODO Auto-generated catch block
    e.PrintStackTrace();
}

我试图在不牺牲太多质量的情况下找到一种好方法。感谢任何帮助=]

更新:我尝试这样做,反对上述情况,也没有运气。

View v1 = act.RootView;
v1.DrawingCacheQuality = DrawingCacheQuality.Low;
v1.DrawingCacheEnabled = true;
Bitmap bitmap = Bitmap.CreateScaledBitmap(v1.GetDrawingCache(true),720,1280,false);
v1.DrawingCacheEnabled = false;
v1.DestroyDrawingCache();
v1.Dispose();

1 个答案:

答案 0 :(得分:0)

我最终使用了:

// create bitmap screen capture
View v1 = act.RootView;
Bitmap bitmap = Bitmap.CreateBitmap(720,1280,Bitmap.Config.Argb8888);
Canvas can = new Canvas(bitmap);
v1.Draw(can);

而不是我更新的代码。这有助于获得我需要的屏幕截图并降低内存使用率。