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

Add support for pasting images from clipboard #457

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion android/app/src/main/kotlin/com/apps/blt/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -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()
}
}
}
}
16 changes: 13 additions & 3 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -17,3 +15,15 @@ void main() async {
appRunner: () => runApp(BLT()),
);
}

const platform = MethodChannel('com.apps.blt/image_paste');

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;
}
}
42 changes: 19 additions & 23 deletions lib/src/pages/home/report_bug.dart
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -88,28 +87,16 @@ class _ReportFormState extends ConsumerState<ReportForm> {
}
}

// Future<File> _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<void> _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<void> _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;
Expand Down Expand Up @@ -639,6 +626,15 @@ class _ReportFormState extends ConsumerState<ReportForm> {
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(
Expand Down
Loading