diff --git a/android/app/src/main/kotlin/com/apps/blt/MainActivity.kt b/android/app/src/main/kotlin/com/apps/blt/MainActivity.kt index b4f2ed6aee..c9a1abd463 100644 --- a/android/app/src/main/kotlin/com/apps/blt/MainActivity.kt +++ b/android/app/src/main/kotlin/com/apps/blt/MainActivity.kt @@ -1,6 +1,44 @@ package com.apps.blt +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() { +class MainActivity : FlutterActivity() { + private val CHANNEL = "com.apps.blt/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() + } + } + } } diff --git a/lib/main.dart b/lib/main.dart index e686413751..6f609c1c6a 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,13 +1,11 @@ -//import 'package:blt/src/util/services/init_service.dart'; import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; import 'src/app.dart'; -//import 'package:flutter/services.dart'; import 'package:sentry_flutter/sentry_flutter.dart'; // Application's entry point void main() async { WidgetsFlutterBinding.ensureInitialized(); - //InitService.init(); // Call the init method from the InitService class const SENTRY_DSN = "https://example-234234324.com/4504877879197696"; await SentryFlutter.init( (options) { @@ -17,3 +15,15 @@ void main() async { appRunner: () => runApp(BLT()), ); } + +const platform = MethodChannel('com.apps.blt/image_paste'); + +Future _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; + } +} diff --git a/lib/src/pages/home/report_bug.dart b/lib/src/pages/home/report_bug.dart index 498140145a..f15d862e60 100644 --- a/lib/src/pages/home/report_bug.dart +++ b/lib/src/pages/home/report_bug.dart @@ -1,4 +1,3 @@ -// ignore_for_file: public_member_api_docs, sort_constructors_first import 'package:blt/src/pages/home/home_imports.dart'; import 'package:permission_handler/permission_handler.dart'; @@ -88,28 +87,16 @@ class _ReportFormState extends ConsumerState { } } - // Future _coverToImage(Uint8List imageBytes) async { - // String tempPath = (await getTemporaryDirectory()).path; - // File file = File('$tempPath/profile.png'); - // await file.writeAsBytes(imageBytes.buffer - // .asUint8List(imageBytes.offsetInBytes, imageBytes.lengthInBytes)); - // return file; - // } - - // Future _pasteImageFromClipBoard() async { - // try { - // final imageBytes = await Pasteboard.image; - // late File? image; - // if (imageBytes != null) { - // image = await _coverToImage(imageBytes); - // } - // setState(() { - // _image = image; - // }); - // } catch (e) { - // print('No Image Found On Clipboard'); - // } - // } + Future _pasteImageFromClipboard() async { + Uint8List? imageBytes = await _getImageFromClipboard(); + if (imageBytes != null) { + setState(() { + _image.add(File.fromRawPath(imageBytes)); + }); + } else { + print('No Image Found On Clipboard'); + } + } void markdownFormatting(String formatter) { int start = _descriptionController.selection.baseOffset; @@ -639,6 +626,15 @@ class _ReportFormState extends ConsumerState { hintText: AppLocalizations.of(context)! .descriptio, border: InputBorder.none, + contentInsertionConfiguration: + ContentInsertionConfiguration( + onContentInserted: (content) { + if (content.mimeType == "image/png") { + _pasteImageFromClipboard(); + } + }, + allowedMimeTypes: ["image/png"], + ), ), style: GoogleFonts.aBeeZee( textStyle: TextStyle(