How Native Extension take screenshot on Android device?
Asked Answered
V

2

8

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.

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. My java code runs exactly on Android Java Application, but returns black image on Adobe Air Android Application with Native Extension.

Is there any solution or any way to take a screenshot using Java in NativeExtension?

Thanks much!

Vahe answered 16/1, 2014 at 14:20 Comment(6)
I'm having the same problem! my iOS ANE works fine though. anyone know how to fix the black image problem?Send
Why use an ANE when you can do this with pure action script? From what I understand you wish to get a screenshot of the stage on demand correct?Dyspeptic
Agree, you could do this with pure AS3 inside AIR. Would this be a acceptable with a code example?Deliladelilah
would be acceptable as a comment but this would not be a correct answer. Besides, there's virtually not a correct way to do this in pure AS3 since a mix of classic displayobject and Stage3D would make taking a screenshot with only as3 rather difficult.Laconism
@Vahe do you need the ANE to return an image object, or would a uri to a local file be acceptable?Porche
@sudoplz, I've tried using AS3 only--it results in a noticeable freeze/delay effect that is not a good user experience. writing a native extension for iOS results in a must faster screenshot.Send
M
1

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; 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.

Maximilianus answered 11/7, 2014 at 18:34 Comment(2)
Try removing the buildDrawingCache() line; that can sometimes cause issues, and from what I've read it's not completely necessary.Maximilianus
Also, check if image is null, that could be why you're just seeing black.Maximilianus
P
0

You can take a screenshot like this and save it to the SD card:

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):

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
Probe answered 9/7, 2014 at 6:32 Comment(2)
@Laconism Dunno if this is worth a down vote, this almost achieves a correct solution. If this returned file.getAbsolutePath() as a FREObject it would work.Porche
But this is an ANE question. PO is able to take a screenshot using Android based code, answer gives another way to take a screenshot on Android, big deal. Question is about wrapping it all up in an ANE, answer is off subject.Laconism

© 2022 - 2024 — McMap. All rights reserved.