I have GOT to stop posting when it is late and I am tired. I did not include the inline Java code when I copied the code from the link listed above:
#If Java
import android.app.Activity;
import android.graphics.Bitmap;
import android.view.ViewGroup;
public static Bitmap takeScreenshot2(Activity activity) {
ViewGroup decor = (ViewGroup) activity.getWindow().getDecorView();
ViewGroup decorChild = (ViewGroup) decor.getChildAt(0);
decorChild.setDrawingCacheEnabled(true);
decorChild.buildDrawingCache();
Bitmap drawingCache = decorChild.getDrawingCache(true);
Bitmap bitmap = Bitmap.createBitmap(d rawingCache);
decorChild.setDrawingCacheEnabled(false);
return bitmap;
}
#End If
But that brought up a new set of errors which I was finally able to resolve. But only worked in main. But eventually I was able to make it work in a standalone routine. I have posted a project which works, but also has a bunch of commented code some which also works. The relevant code is this:
Dim NM As JavaObject
NM.InitializeContext
Dim MENM As JavaObject = Me
B4XImageView1.Bitmap = MENM.RunMethod("takeScreenshot2", Array(NM))
Took me a lot of monkeys and typewriters work to figure this out. Hopefully someday someone can explain to me what .InitializeContext gets you vs Me.
The ME came from one of Erel's examples showing how to run java code from a Class Module"
'Class module
Sub Class_Globals
Private nativeMe As JavaObject
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
nativeMe = Me
Log(nativeMe.RunMethod("Multiply", Array (10)))
End Sub
#If JAVA
private int mm = 100;
public int Multiply(int i) {
return i * 2 + mm;
}
#End If
But of course this did not work for me because the array needed to be a javaobject, not a number and needed to be .InitializeContext , not Me.
I'm also not sure if what I did is legal and reliable. All the examples I find have the java object in Process_Globals and the .initializecontext or = Me in Activity_Create or Initialize. I moved these into my Sub because that is the only place I need them. I also don't understand why if run from Main this works:
Sub Globals
Private nativeMe As JavaObject
End Sub
Sub Activity_Create(FirstTime As Boolean)
Dim pm As B4XPagesManager
pm.Initialize(Activity)
If FirstTime Then
nativeMe.InitializeContext
End If
End Sub
.
.
.
Public Sub Screenshot(B4XImgV As B4XImageView)
B4XImageView1.Bitmap = nativeMe.RunMethod("takeScreenshot", Array(nativeMe))
End Sub
That is nativeMe.....nativeMe but when running it from MainPage RunMethod needs to be JavaObject = Me and the Array needs to be JavaObject.InitializeContext.
Just because it is what I was after I threw in the code to crop the screenshot to a specific control, which is what I was after:
Dim Dims(2) As Int
Dim B As JavaObject = Button1
B.RunMethod("getLocationOnScreen", Array As Object(Dims))
B4XImageView1.Bitmap = B4XImageView1.Bitmap.Crop(Dims(0), Dims(1), Button1.Width, Button1.Height)
B4XImageView1.ResizeMode = "FILL_NO_DISTORTIONS"
Now, "getLocationOnScreen" is, evidently, Java code that is built in to the control? Go figure. No inline code required.
Anyway, thanks for your help. A program that works is attached. At least I hope it works, I posted this when it is late and I am tired ?? ?