B4A Library [B4X][B4Xlib] B4XClipboard

Theera

Well-Known Member
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: 5
  • B4XClipboard.bas
    3 KB · Views: 5
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: 6

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