简体   繁体   English

Native Extension如何在Android设备上拍摄屏幕截图?

[英]How Native Extension take screenshot on Android device?

I have a Adobe Air application that intend to take a screenshot with Native Extension on Android device, but the java code returns a black image. 我有一个Adobe Air应用程序,打算在Android设备上使用Native Extension截屏,但是Java代码返回黑色图像。

public FREObject call(FREContext context, FREObject[] params) {
    View view = context.getActivity().getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    Bitmap image = view.getDrawingCache();
}

I don't know much about Adobe Air. 我对Adobe Air不太了解。 My java code runs exactly on Android Java Application, but returns black image on Adobe Air Android Application with Native Extension. 我的Java代码完全在Android Java应用程序上运行,但在具有Native Extension的Adobe Air Android应用程序上返回黑色图像。

Is there any solution or any way to take a screenshot using Java in NativeExtension? 是否有任何解决方案或任何方法在NativeExtension中使用Java截屏?

Thanks much! 非常感谢!

Could be that you are not getting the correct view. 可能是您没有获得正确的视图。 Try this to get the topmost root view. 尝试此操作以获取最顶层的根视图。

public FREObject call(FREContext context, FREObject[] params)
{
    View view = findViewById(android.R.id.content).getRootView();
    view.setDrawingCacheEnabled(true);
    Bitmap image = view.getDrawingCache();
    if(image == null)
    {
        System.out.println("Image returned was null!");
    }
}

I also removed the buildDrawingCache() line; 我还删除了buildDrawingCache()行; that can sometimes cause issues, and from what I've read it's not completely necessary. 有时可能会导致问题,根据我所读的内容,这并非完全必要。

Finally you'll want to check if the bitmap being returned is null. 最后,您将要检查返回的位图是否为空。 If so that could be why it's all black. 如果是这样,那可能就是为什么全黑的原因。

You can take a screenshot like this and save it to the SD card: 您可以像这样截屏,然后将其保存到SD卡中:

View content = findViewById(R.id.layoutroot);
content.setDrawingCacheEnabled(true);
Function to get the rendered view:
private void getScreen()
{
    View content = findViewById(R.id.layoutroot);
    Bitmap bitmap = content.getDrawingCache();
    File file = new File( Environment.getExternalStorageDirectory() + "/asdf.png");
    try 
    {
        file.createNewFile();
        FileOutputStream ostream = new FileOutputStream(file);
        bitmap.compress(CompressFormat.PNG, 100, ostream);
        ostream.close();
    } 
    catch (Exception e) 
    {
        e.printStackTrace();
    }
}

You have to add this permission to your AndroidManifest (if you want to save it): 您必须将此权限添加到您的AndroidManifest中(如果要保存它):

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM