B4A Library [B4X][B4Xlib] B4XClipboard

As the name says, crossplatform (B4A, B4J, B4i) clipboard.

For B4A I used the code from @hatzisn's class and I thank him for sharing it.

Updated to version 2.0.0. Great work, @Alexander Stolte, thank you.

  • B4XClipboard
    • Functions:
      • hasImage As Boolean
      • HasText As Boolean
      • Initialize
        Initializes the object. You can add parameters to this method if needed.
    • Properties:
      • Image As B4XBitmap
      • Text As String
 

Attachments

  • B4XClipboard.b4xlib
    1.8 KB · Views: 118
Last edited:

Theera

Expert
Licensed User
Longtime User
Hi LucaMs,
Is there example for testing?
 

Alexander Stolte

Expert
Licensed User
Longtime User
The clipboard library under ios can also store images in the clipboard:
B4X:
Public Sub setImage(Img As B4XBitmap)
    #If B4I
    iClipboard.ImageItem = Img
    #Else
    mBitmap = Img
    #End If
End Sub
Public Sub getImage As B4XBitmap
    #If B4I
    Return iClipboard.ImageItem
    #Else
    Return mBitmap
    #End If
End Sub
Public Sub hasImage As Boolean
    #If B4I
    Return (iClipboard.ImageItem.IsInitialized And iClipboard.ImageItem <> Null)
    #Else
    Return (mBitmap.IsInitialized And mBitmap <> Null)
    #End If
End Sub

and the example project under B4I had no views in the mainpage

I have included both in the attachment

Maybe you should mention in the property description that in B4A and B4J the image clipboard is not global, but only within the class
 

Attachments

  • B4XClipboard.zip
    183.1 KB · Views: 80
  • B4XClipboard.bas
    3 KB · Views: 74
Last edited:

LucaMs

Expert
Licensed User
Longtime User
Maybe you should mention in the property description that in B4A and B4J the image clipboard is not global, but only within the class
It could probably be implemented well in B4J and B4A too, i.e. not as a "local" B4XBitmap, in the class.

There is fx.Clipboard.SetImage but the parameter must be of type Image, you can't pass it a B4XBitmap. I should find a way to do the conversion. You can assign an Image to a B4XBitmap but not vice versa.

Even with B4A, by modifying the Java code, I think it is possible to create the set and get Image methods (B4XBitmp)
 

Alexander Stolte

Expert
Licensed User
Longtime User
There is fx.Clipboard.SetImage but the parameter must be of type Image, you can't pass it a B4XBitmap. I should find a way to do the conversion. You can assign an Image to a B4XBitmap but not vice versa.
This works for me in B4J:
B4X:
Public Sub setImage(Img As B4XBitmap)
    #If B4I
    iClipboard.ImageItem = Img
    #Else If B4J
    Dim bitmap As Image = Img
    fx.Clipboard.SetImage(bitmap)
    #Else
    mBitmap = Img
    #End If
End Sub
Public Sub getImage As B4XBitmap
    #If B4I
    Return iClipboard.ImageItem
    #Else if B4J
    Return fx.Clipboard.GetImage
    #Else
    Return mBitmap
    #End If
End Sub
Public Sub hasImage As Boolean
    #If B4I
    Return (iClipboard.ImageItem.IsInitialized And iClipboard.ImageItem <> Null)
    #Else if B4J
    Return fx.Clipboard.HasImage
    #Else
    Return (mBitmap.IsInitialized And mBitmap <> Null)
    #End If
End Sub
 

LucaMs

Expert
Licensed User
Longtime User
This works for me in B4J:
B4X:
Public Sub setImage(Img As B4XBitmap)
    #If B4I
    iClipboard.ImageItem = Img
    #Else If B4J
    Dim bitmap As Image = Img
    fx.Clipboard.SetImage(bitmap)
    #Else
    mBitmap = Img
    #End If
End Sub
Public Sub getImage As B4XBitmap
    #If B4I
    Return iClipboard.ImageItem
    #Else if B4J
    Return fx.Clipboard.GetImage
    #Else
    Return mBitmap
    #End If
End Sub
Public Sub hasImage As Boolean
    #If B4I
    Return (iClipboard.ImageItem.IsInitialized And iClipboard.ImageItem <> Null)
    #Else if B4J
    Return fx.Clipboard.HasImage
    #Else
    Return (mBitmap.IsInitialized And mBitmap <> Null)
    #End If
End Sub
Now all that's missing is the "pure" implementation for B4A but... is it worth it?
 

Alexander Stolte

Expert
Licensed User
Longtime User
Unfortunately I can't test the code because my android phone is at home, but maybe it will work:
B4X:
#IF B4A
#IF JAVA

import android.content.ClipboardManager;
import android.content.ClipData;
import android.content.Context;
import android.content.ClipDescription;
import android.graphics.Bitmap;
import android.net.Uri;
import java.io.File;
import java.io.FileOutputStream;
import android.graphics.BitmapFactory;

public Boolean SetTextToClipboard(Object objtxt, Object mObjContext) {
    try {
        String txt = (String) objtxt;
        Context mContext = (Context) mObjContext;
        ClipboardManager myClipboard = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData myClip;
        myClip = ClipData.newPlainText("text", txt);
        myClipboard.setPrimaryClip(myClip);
        return true;
    } catch (Exception ex) {
        return false;
    }
}

public String GetTextFromClipboard(Object mObjContext) {
    Context mContext = (Context) mObjContext;
    ClipboardManager myClipboard = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
    ClipData myClip = myClipboard.getPrimaryClip();
    ClipData.Item item = myClip.getItemAt(0);
    String txt = item.getText().toString();
    return txt;
}

public Boolean HasTextCopied(Object mObjContext) {
    Context mContext = (Context) mObjContext;
    ClipboardManager myClipboard = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
    if (!(myClipboard.hasPrimaryClip())) {
        return false;
    } else if (!(myClipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN))) {
        return false;
    } else {
        return true;
    }
}

public Boolean HasBitmapCopied(Object mObjContext) {
    Context mContext = (Context) mObjContext;
    ClipboardManager myClipboard = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);

    if (!myClipboard.hasPrimaryClip()) {
        return false;
    } else if (!myClipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_URILIST)) {
        return false;
    } else {
        return true;
    }
}

public Bitmap GetBitmapFromClipboard(Object mObjContext) {
    try {
        Context mContext = (Context) mObjContext;
        ClipboardManager myClipboard = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);

        if (!myClipboard.hasPrimaryClip()) {
            return null;
        }

        ClipData clipData = myClipboard.getPrimaryClip();
        ClipData.Item item = clipData.getItemAt(0);

        // Prüfen, ob die URI vorhanden ist
        Uri imageUri = item.getUri();
        if (imageUri != null) {
            // Bitmap aus der URI laden
            return BitmapFactory.decodeStream(mContext.getContentResolver().openInputStream(imageUri));
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

public Boolean SetBitmapToClipboard(Bitmap bitmap, Object mObjContext) {
    try {
        Context mContext = (Context) mObjContext;
        File cachePath = new File(mContext.getCacheDir(), "images");
        cachePath.mkdirs(); // Erstellen Sie das Verzeichnis, falls es nicht existiert
        File file = new File(cachePath, "image.png");
        FileOutputStream fos = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.close();

        // URI erstellen
        Uri imageUri = Uri.fromFile(file);

        // Bild in die Zwischenablage legen
        ClipboardManager myClipboard = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData myClip = ClipData.newUri(mContext.getContentResolver(), "Image", imageUri);
        myClipboard.setPrimaryClip(myClip);
        return true;
    } catch (Exception ex) {
        return false;
    }
}

#End If
#End If

B4X:
Public Sub setImage(Img As B4XBitmap)
    #If B4I
    iClipboard.ImageItem = Img
    #Else If B4J
    Dim bitmap As Image = Img
    fx.Clipboard.SetImage(bitmap)
    #Else
    Dim r As Reflector
    Dim jo As JavaObject
    jo = Me
    jo.RunMethod("SetBitmapToClipboard", Array As Object(Img, r.GetContext))
    #End If
End Sub
Public Sub getImage As B4XBitmap
    #If B4I
    Return iClipboard.ImageItem
    #Else if B4J
    Return fx.Clipboard.GetImage
    #Else
    Dim r As Reflector
    Dim jo As JavaObject
    jo = Me
    Return jo.RunMethod("GetBitmapFromClipboard", Array As Object(r.GetContext))
    #End If
End Sub
Public Sub hasImage As Boolean
    #If B4I
    Return (iClipboard.ImageItem.IsInitialized And iClipboard.ImageItem <> Null)
    #Else if B4J
    Return fx.Clipboard.HasImage
    #Else
    
    Dim jo As JavaObject
    Dim r As Reflector
    jo = Me
    Dim bOk As Boolean
    bOk = jo.RunMethod("HasBitmapCopied", Array As Object(r.GetContext))
    Return bOk
    
    #End If
End Sub
 

Attachments

  • B4XClipboard.bas
    6 KB · Views: 82

LucaMs

Expert
Licensed User
Longtime User
Unfortunately I can't test the code because my android phone is at home, but maybe it will work:
No, there is some error:

1737986958953.png


but for now I'm not looking for it.

Lucky you who are not at home but at the seaside 😂
 

Alexander Stolte

Expert
Licensed User
Longtime User
No, there is some error:
strange, I don't have an error when compiling, but I also use jdk-14.0.1 and B4A 12.80. I'll have a look when I get home.

Lucky you who are not at home but at the seaside 😂
I'm in the office, 15 minutes' walk from home 🥸
 

LucaMs

Expert
Licensed User
Longtime User
Most likely my mistake. Only now I looked at the image I attached and I see NewClipboard, which WAS another test class.
I removed it from the B4J project but not from the B4A project.
In fact now the compilation is successful.
But something is wrong; I have the following B4A code in the test project:
B4X:
    xClipboard.Image = B4XImageView1.Bitmap
    Log("has image: " & xClipboard.hasImage)
    
    If xClipboard.hasImage Then
        Dim xBmp As B4XBitmap
        xBmp = xClipboard.Image
        B4XImageView2.Bitmap = xBmp
    End If
and hasImage returns False (B4XImageView1 has a B4XBitmap)
 

Alexander Stolte

Expert
Licensed User
Longtime User
It works now in B4A.
We use FileProvider to generate a tmp image and create an uri to copy this uri to the clipboard.

Please read the FileProvider setup instructions:

My Android phone recognizes that I have placed a picture in the clipboard and immediately offers me further options
 

Attachments

  • B4XClipboard.zip
    195.6 KB · Views: 72
  • B4XClipboard.bas
    4.9 KB · Views: 73

LucaMs

Expert
Licensed User
Longtime User
It works now in B4A.
We use FileProvider to generate a tmp image and create an uri to copy this uri to the clipboard.

Please read the FileProvider setup instructions:

My Android phone recognizes that I have placed a picture in the clipboard and immediately offers me further options
Perfect.

Now, however, there is another problem: how can I consider the library my creation since you and @hatzisn did everything? :confused: 😄
 

Alexander Stolte

Expert
Licensed User
Longtime User
Now, however, there is another problem: how can I consider the library my creation since you and @hatzisn did everything? :confused: 😄
mention us both in the 1st thread and say thank you for the help :D

Here was another error that I fixed
 

Attachments

  • B4XClipboard.bas
    4.9 KB · Views: 69

Alexander Stolte

Expert
Licensed User
Longtime User
Maybe you can this to the first post:
B4XClipboard
Author:
LucaMs
Version: 2.00
  • B4XClipboard
    • Functions:
      • hasImage As Boolean
      • HasText As Boolean
      • Initialize
        Initializes the object. You can add parameters to this method if needed.
    • Properties:
      • Image As B4XBitmap
      • Text As String
so that people get an overview of what the class has for properties and then also achieve better results in the B4X search
 

LucaMs

Expert
Licensed User
Longtime User
Maybe you can this to the first post:
B4XClipboard
Author:
LucaMs
Version: 2.00
  • B4XClipboard
    • Functions:
      • hasImage As Boolean
      • HasText As Boolean
      • Initialize
        Initializes the object. You can add parameters to this method if needed.
    • Properties:
      • Image As B4XBitmap
      • Text As String
so that people get an overview of what the class has for properties and then also achieve better results in the B4X search
You make me work too much! Now I'll even have to copy and paste!
😄
 
Top