Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2️⃣6️⃣ allow the user to be able to past an image into the bug report page in the app #231

Closed
fredfalcon opened this issue Mar 5, 2023 · 11 comments · Fixed by #461
Assignees
Labels
good first issue Good for newcomers

Comments

@fredfalcon
Copy link
Contributor

fredfalcon commented Mar 5, 2023

Need custom function for copying image from clipboard as Pasteboard package does not supports android platform

to get this functionality switch to the flutter master channel (command flutter channel master), this will now include PR 110052 which adds this functionality.

to use it simply add this to your TextField:

//...
contentInsertionConfiguration: ContentInsertionConfiguration(
onContentInserted : (_){/your cb here/}
allowedMimeTypes: ["image/png",/.../],
}),
//...
this will then use the android specific content commit api to (as you guessed) tell the OS that your TextField accepts the given mimetypes; gboard will recognize this and suggest the content with fitting mimetypes in your clipboard

@fredfalcon fredfalcon transferred this issue from another repository Mar 6, 2023
@fredfalcon fredfalcon transferred this issue from OWASP-BLT/BLT Mar 7, 2023
@letsintegreat
Copy link
Contributor

Regarding this, I did some research but could not find anything for android. Therefore, 12 days ago I started a thread on Stack Overflow, but it went unanswered. I have now started a bounty on that thread, hopefully we will get something now - https://stackoverflow.com/questions/75658357/how-to-accept-images-in-textfield-flutter

@fredfalcon
Copy link
Contributor Author

Please excuse the un formatted AI response

Currently, Flutter's TextField widget does not have built-in support for pasting images directly from the clipboard. However, you can achieve this functionality by implementing a workaround using a combination of platform channels and a third-party package.

Here's a step-by-step guide to implementing this feature:

Create a MethodChannel to communicate with native Android code:
First, you'll need to create a platform channel for invoking native Android methods from Flutter. In your main.dart file, add the following code:

dart
Copy code
import 'package:flutter/services.dart';

const platform = const MethodChannel('com.example.app/image_paste');
Replace com.example.app with your app's package name.

Implement the native Android code for accessing clipboard images:
In your Android project, navigate to the MainActivity.kt file and add the following code:

kotlin
Copy code
package com.example.app

import android.content.ClipData
import android.content.ClipDescription
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import androidx.annotation.NonNull
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import java.io.ByteArrayOutputStream

class MainActivity : FlutterActivity() {
private val CHANNEL = "com.example.app/image_paste"

override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
    super.configureFlutterEngine(flutterEngine)
    MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
        when (call.method) {
            "getImageFromClipboard" -> {
                val clipboard = getSystemService(CLIPBOARD_SERVICE) as android.content.ClipboardManager
                if (clipboard.hasPrimaryClip() && clipboard.primaryClipDescription?.hasMimeType(ClipDescription.MIMETYPE_TEXT_HTML) == true) {
                    val item: ClipData.Item = clipboard.primaryClip!!.getItemAt(0)
                    val imageUri = item.uri

                    // Read image from URI
                    val inputStream = contentResolver.openInputStream(imageUri)
                    val bitmap = BitmapFactory.decodeStream(inputStream)

                    // Convert bitmap to PNG byte array
                    val byteArrayOutputStream = ByteArrayOutputStream()
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream)
                    val byteArray: ByteArray = byteArrayOutputStream.toByteArray()

                    result.success(byteArray)
                } else {
                    result.success(null)
                }
            }
            else -> result.notImplemented()
        }
    }
}

}
Replace com.example.app with your app's package name.

Handle pasting images in Flutter:
Now, you can create a button in your Flutter app that fetches images from the clipboard and displays them. Use the Image.memory widget to display the image bytes.

dart
Copy code
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

// ...

Future<Uint8List?> _getImageFromClipboard() async {
try {
final Uint8List? imageBytes = await platform.invokeMethod('getImageFromClipboard');
return imageBytes;
} on PlatformException catch (e) {
print('Failed to get image from clipboard: ${e.message}');
return null;
}
}

// ...

ElevatedButton(
onPressed: () async {
Uint8List? imageBytes = await _getImageFromClipboard();
if (imageBytes != null) {
setState(() {
_image = Image.memory(imageBytes);
});
}
},
child: Text('Paste Image'),
),

// ...

_image, // display the pasted image
Replace _image with a variable of type Image? in your widget's state.

This implementation allows you to paste images from the clipboard into your Flutter app

@letsintegreat
Copy link
Contributor

This is interesting, let me give it a shot!

@letsintegreat
Copy link
Contributor

@fredfalcon It doesn't work, getImageFromClipboard always returns null.

@fredfalcon
Copy link
Contributor Author

You could try giving the error and code to ChatGPT4 and work it out. I'm rate limited currently

@DonnieBLT DonnieBLT changed the title Need custom function for copying image from clipboard as Pasteboard package does not supports android platform Project: APP > Paste Image Jan 27, 2024
@DonnieBLT DonnieBLT changed the title Project: APP > Paste Image Project: Flutter > Paste Image Jan 27, 2024
@github-project-automation github-project-automation bot moved this to Backlog in 📌 All Mar 1, 2024
@DonnieBLT DonnieBLT changed the title Project: Flutter > Paste Image allow the user to be able to past an image into the bug report page Mar 3, 2024
@DonnieBLT DonnieBLT removed the project label Mar 3, 2024
@DonnieBLT DonnieBLT changed the title allow the user to be able to past an image into the bug report page allow the user to be able to past an image into the bug report page in the app Mar 3, 2024
@DonnieBLT DonnieBLT moved this from Backlog to Ready in 📌 All Mar 3, 2024
@DonnieBLT DonnieBLT added the good first issue Good for newcomers label Jun 1, 2024
@Uttkarsh-raj Uttkarsh-raj removed their assignment Jun 4, 2024
@DonnieBLT DonnieBLT changed the title allow the user to be able to past an image into the bug report page in the app 2️⃣6️⃣ allow the user to be able to past an image into the bug report page in the app Jul 21, 2024
DonnieBLT added a commit that referenced this issue Oct 16, 2024
@krrish-sehgal
Copy link
Contributor

krrish-sehgal commented Oct 23, 2024

Is this issue resolved?
if not , i would like to work on this.

@DonnieBLT
Copy link
Collaborator

It’s not resolved yet

@krrish-sehgal
Copy link
Contributor

/assign

Copy link
Contributor

Hello @krrish-sehgal! You've been assigned to OWASP-BLT/BLT-Flutter. You have 24 hours to complete a pull request. To place a bid and potentially earn some BCH, type /bid [amount in BCH] [BCH address].

@krrish-sehgal
Copy link
Contributor

Just to clarify, when implementing the image-pasting option in the description text box, should the pasted image be displayed at the bottom of the description (similar to adding an image through the "Add Image" button), or should it appear at the top of the description box, like in GPT's text field?

@krrish-sehgal
Copy link
Contributor

@letsintegreat , I was able to do it using the approach mentioned by @fredfalcon, with changes that deemed necessary.
It would be highly helpful if you could review my PR #461.

Thanks.

@github-project-automation github-project-automation bot moved this from Ready to Done in 📌 All Oct 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

5 participants