Save image issue

Asmoro

Active Member
Licensed User
Longtime User
Hi all,

I made a test prog which a screen will be copied to the sdcard and
to display on a listview or a scrollview afterwards by using a search input.

But I'm stuck with the 'copy' part.
It shows only a black image.

Strange thing is that I don't get any errors while compiling the prog.

Can anyone show me the right direction?

Asmoro

note: a testprogram attached to look at it.

edit: sorry my mistake, I do get errors as 'java.lang.NullPointerException' for example when running the prog.
edit 2: fixed the errors but still black screen image
 

Attachments

  • SaveImageTest.zip
    7.9 KB · Views: 198
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
The following code is wrong:
B4X:
    Canvas1.Initialize(Activity)

    Bitmap1.Initialize3(Canvas1.Bitmap)
    
    DestRect.Initialize(0dip, 0dip, 320dip, 320dip)
    
    Canvas1.DrawBitmap(Bitmap1, Null, DestRect)      
    
    Out = File.OpenOutput(File.DirRootExternal, "Snapshot.jpg", False)
    Canvas1.Bitmap.WriteToStream(Out, 100, "JPEG")
    Out.Close
You are creating a bitmap which is a copy of the Activity background and then you draw it over the activity. It doesn't really do anything.

I guess that the activity background is black and therefore you get a black image.
 
Upvote 0

Asmoro

Active Member
Licensed User
Longtime User
Thanks for your reply Erel.

In the meantime I made some adjustments and still didn't saw the views, accept the panel (now in white and the views are linked with the panel in designer)

I wonder if it's possible what I'm trying to achieve or do I need another coding for this.

note: the views on the panel are displaying input results from the main screen
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The activity background doesn't include the views. You will need to manually draw the views on the background. You can use Canvas.DrawDrawable to draw views:
B4X:
   Dim v As View
   Dim Canvas As Canvas
   Dim Rect As Rect
   Rect.Initialize(v.Left, v.Top, v.Left + v.Width, v.Top + v.Height)
   Canvas.DrawDrawable(v.Background, Rect)
 
Upvote 0

Asmoro

Active Member
Licensed User
Longtime User
Hi Erel,

I tried with your example to code, but somehow I'm not allow to be able to initialize the view (label1) within b4a.

Someone mention that it will be fixed for Canvas.DrawDrawable.

If that's not the case, what could be the cause.

Below the error logcode.

B4X:
LogCat connected to: emulator-5554
** Activity (main) Create, isFirst = true **


** Activity (main) Resume **


** Activity (main) Pause, UserClosed = false **
saveimage_globals (B4A line: 22)
Canvas1.DrawDrawable(label1, Rect)



java.lang.RuntimeException: Object should first be initialized (View).
   at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:45)
   at anywheresoftware.b4a.objects.ViewWrapper.getBackground(ViewWrapper.java:71)
   at com.asmoro.saveimage.saveimage._globals(saveimage.java:318)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:105)
   at com.asmoro.saveimage.saveimage.initializeGlobals(saveimage.java:201)
   at com.asmoro.saveimage.saveimage.afterFirstLayout(saveimage.java:81)
   at com.asmoro.saveimage.saveimage.access$100(saveimage.java:16)
   at com.asmoro.saveimage.saveimage$WaitForLayout.run(saveimage.java:72)
   at android.os.Handler.handleCallback(Handler.java:587)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:123)
   at android.app.ActivityThread.main(ActivityThread.java:3683)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
   at dalvik.system.NativeStart.main(Native Method)
java.lang.RuntimeException: Object should first be initialized (View).
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Create, isFirst = true **


** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
saveimage_globals (B4A line: 22)


Canvas1.DrawDrawable(label1.Background, Rect)

java.lang.RuntimeException: Object should first be initialized (View).
   at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:45)
   at anywheresoftware.b4a.objects.ViewWrapper.getBackground(ViewWrapper.java:71)
   at com.asmoro.saveimage.saveimage._globals(saveimage.java:318)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:105)
   at com.asmoro.saveimage.saveimage.initializeGlobals(saveimage.java:201)
   at com.asmoro.saveimage.saveimage.afterFirstLayout(saveimage.java:81)
   at com.asmoro.saveimage.saveimage.access$100(saveimage.java:16)
   at com.asmoro.saveimage.saveimage$WaitForLayout.run(saveimage.java:72)
   at android.os.Handler.handleCallback(Handler.java:587)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:123)
   at android.app.ActivityThread.main(ActivityThread.java:3683)


   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
   at dalvik.system.NativeStart.main(Native Method)
java.lang.RuntimeException: Object should first be initialized (View).
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **

This is the module:

B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   
   Dim result As String
   result = ""
   
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   
   Dim ImageView1 As ImageView
   Dim Button1 As Button
   
   Dim label1 As View
   Dim Canvas1 As Canvas
   Dim Rect As Rect
   Rect.Initialize(30, 30, 30 + 200, 30 + 65)
   Canvas1.DrawDrawable(label1, Rect)
   Activity.Invalidate
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("SaveImage.bal")
   
  
End Sub

Sub Activity_Resume

   'Label1.Text = "" & Main.Input1
   'Label2.Text = "" & Main.Input2
   'Label3.Text = "" & Main.Input3

   If Main.ImageDir<>"" Then
      ImageView1.Bitmap = (LoadBitmapSample(Main.ImageDir, Main.Image1, 200, 200))
   End If

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
   
   Dim out As OutputStream
    Out = File.OpenOutput(File.DirRootExternal, "Snapshot.jpg", False)
    Canvas1.Bitmap.WriteToStream(Out, 100, "JPEG")
    Out.Close

   
End Sub
 
Last edited:
Upvote 0

Asmoro

Active Member
Licensed User
Longtime User
Sorry, changed in
B4X:
 tags and put the code on sub button_1 click.

Didn't change the problem unfortunately.
Trying once again.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Is this the updated code, it still contains invalid code:
B4X:
Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   
   Dim ImageView1 As ImageView
   Dim Button1 As Button
   
   Dim label1 As View
   Dim Canvas1 As Canvas
   Dim Rect As Rect
   Rect.Initialize(30, 30, 30 + 200, 30 + 65)
   Canvas1.DrawDrawable(label1, Rect)
   Activity.Invalidate
End Sub
 
Upvote 0

Asmoro

Active Member
Licensed User
Longtime User
Hi Erel,

No I put the code afterwards under the 'Sub Button1_Click' , like:

B4X:
Sub Button1_Click

   Dim label1 As View
   Dim Canvas1 As Canvas
   Dim Rect As Rect
   Rect.Initialize(30, 30, 30 + 200, 30 + 65)
   Canvas1.DrawDrawable(label1.Background, Rect)
   Activity.Invalidate
   
   Dim out As OutputStream
        Out = File.OpenOutput(File.DirRootExternal, "Snapshot.jpg", False)
        Canvas1.Bitmap.WriteToStream(Out, 100, "JPEG")
        Out.Close

End Sub

If I use 'Canvas1.DrawDrawable(label1.Background, Rect)', it will asking to initializing the view.
When I tried to do that, there was no 'Initialize' to select.

And when I changed to (label1, Rect), I get:

B4X:
Compiling code.                         0.05
Generating R file.                      0.00
Compiling generated Java code.          Error
B4A line: 50
Canvas1.DrawDrawable(label1, Rect)
javac 1.6.0_27
src\com\asmoro\saveimage\saveimage.java:292: inconvertible types
found   : android.view.View
required: android.graphics.drawable.Drawable
_canvas1.DrawDrawable((android.graphics.drawable.Drawable)(_label1.getObject()),(android.graphics.Rect)(_rect.getObject()));
                                                          ^
1 error

So, what to do now?
 
Upvote 0
Top