I am creating a image processing app so first using android platform apis to process the bitmap ad then sending it to flutter. I am using below code to convert bitmap to byte[]
Bitmap bitmap = BitmapFactory.decodeFile(uri.getPath());
ByteBuffer allocate = ByteBuffer.allocate(bitmap.getByteCount());
bitmap.copyPixelsToBuffer(allocate);
byte[] array = allocate.array();
and then sending it to flutter using Method Channel as Uint8List but when i read it as Image.memory() . It is giving me error as
Failed decoding image. Data is either invalid, or it is encoded using an unsupported format. Below is code of My Main Activity
package com.rahul.flutterappdomain;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import com.asksira.bsimagepicker.BSImagePicker;
import com.asksira.bsimagepicker.Utils;
import java.nio.ByteBuffer;
import io.flutter.app.FlutterFragmentActivity;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.GeneratedPluginRegistrant;
public class ActivityMain extends FlutterFragmentActivity implements BSImagePicker.OnSingleImageSelectedListener{
private static final String TAG = "DomainFilterFlutterApp";
private static final String CHANNEL = "com.rummy.io/filter";
MethodChannel methodChannel;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
methodChannel = new MethodChannel(getFlutterView(), CHANNEL);
methodChannel.setMethodCallHandler(new MethodChannel.MethodCallHandler() {
@Override
public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
if (methodCall.method.equals("openImagePicker")) {
showImagePickerDialog();
result.success(null);
}
}
});
}
private void showImagePickerDialog() {
String providerAuthority = "com.rahul.ffd.fileprovider";
BSImagePicker singleSelectionPicker = new BSImagePicker.Builder(providerAuthority)
.setSpanCount(4)
.setGridSpacing(Utils.dp2px(4))
.setPeekHeight(Utils.dp2px(360))
.build();
singleSelectionPicker.show(getSupportFragmentManager(), "picker");
}
@Override
public void onSingleImageSelected(Uri uri) {
Bitmap bitmap = BitmapFactory.decodeFile(uri.getPath());
ByteBuffer allocate = ByteBuffer.allocate(bitmap.getByteCount());
bitmap.copyPixelsToBuffer(allocate);
byte[] array = allocate.array();
methodChannel.invokeMethod("setImage", array);
Log.d(TAG, "onSingleImageSelected: ------------");
}
}
and code of my dart file
import 'dart:async';
import 'dart:io';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'colors.dart';
//import 'package:image_picker/image_picker.dart';
void main(){runApp(MyApp());}
class MyApp extends StatefulWidget {
static const platform = const MethodChannel('com.rummy.io/filter');
String _image;
Uint8List _image_data ;
Future<Null> getImage() async {
platform.invokeMethod("openImagePicker");
}
@override
State<StatefulWidget> createState() {
var myAppState = MyAppState();
platform.setMethodCallHandler((MethodCall call) {
switch (call.method) {
case 'setImage':
print('setState');
myAppState.makeState(call.arguments);
print('setState');
}
});
return myAppState;
}
}
class MyAppState extends State<MyApp> {
void makeState( Uint8List imgData) {
setState(() {
widget._image_data = imgData;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: _mAppTheme,
home: Scaffold(
appBar: AppBar(
actions: <Widget>[
IconButton(
onPressed: () {},
icon: Icon(Icons.save),
)
],
),
body: _mBody(widget._image_data),
floatingActionButton: FloatingActionButton(
onPressed: () {
widget.getImage();
},
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.elliptical(12.0, 13.0))),
child: Icon(Icons.image),
),
),
);
}
double _discreteValue = 0.0;
double _discreteValue2 = 0.0;
Widget _mBody(Uint8List imgData) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Card(
// shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20.0)),
child: imgData == null
? Image.asset(
'images/beauty.jpg',
fit: BoxFit.contain,
)
: /*Image.file(
File(img),
fit: BoxFit.contain,)*/
Image.memory(imgData),
),
),
new Slider(
value: _discreteValue,
min: 0.0,
max: 200.0,
divisions: 10,
activeColor: Colors.greenAccent,
label: '${_discreteValue.round()}',
onChanged: (double value) {
setState(() {
_discreteValue = value;
});}),
new Slider(
value: _discreteValue2,
min: 0.0,
max: 200.0,
divisions: 10,
activeColor: Colors.greenAccent,
label: '${_discreteValue2.round()}',
onChanged: (double value) {
setState(() {
_discreteValue2 = value;
});}),
],
),
);
}
/* Future getImage() async {
var image = await ImagePicker.pickImage(source: ImageSource.gallery);
setState(() {
_image = image;
});
}*/
}
final ThemeData _mAppTheme = _buildAppTheme();
ThemeData _buildAppTheme() {
final ThemeData base = ThemeData.light();
return base.copyWith(
accentColor: kShrineBrown900,
primaryColor: kShrinePink100,
buttonColor: kShrinePink100,
scaffoldBackgroundColor: kShrineBackgroundWhite,
cardColor: kShrineBackgroundWhite,
textSelectionColor: kShrinePink100,
errorColor: kShrineErrorRed,
);
}