Share and Print Screen Flutter - flutter

import 'dart:io';
import 'dart:typed_data';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:esys_flutter_share/esys_flutter_share.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
Imports...
screenshot: ^0.2.0
esys_flutter_share: ^1.0.2
I only need to take a capture, and then share it I am using the following code but I receive the error:
_takeScreenshotandShare() async {
_imageFile = null;
screenshotController
.capture(delay: Duration(milliseconds: 10), pixelRatio: 2.0)
.then((File image) async {
setState(() {
_imageFile = image;
});
final directory = (await getApplicationDocumentsDirectory()).path;
Uint8List pngBytes = _imageFile.readAsBytesSync();
File imgFile = new File('$directory/screenshot.png');
imgFile.writeAsBytes(pngBytes);
print("File Saved to Gallery");
await Share.file('Anupam', 'screenshot.png', pngBytes, 'image/png');
}).catchError((onError) {
print(onError);
});
}
My mistake is:
I/flutter ( 2486): NoSuchMethodError: The method 'findRenderObject' was called on null.
I/flutter ( 2486): Receiver: null
I/flutter ( 2486): Tried calling: findRenderObject()

I face the same issue with screenshot package so my workaround is call same function again incase of error occur.
screenshotController.capture().then((File image) async {
Uint8List pngBytes = image.readAsBytesSync();
final directory = (await getApplicationDocumentsDirectory()).path;
File imgFile = new File('$directory/${DateTime.now().millisecondsSinceEpoch}.png');
await imgFile.writeAsBytes(pngBytes);
if(pngBytes.length == 0)
// call Same function again
else
// your image
}).catchError((onError) {
print(onError);
Future.delayed(Duration(seconds: 1)).then((value) => //call Same function again);
});

Related

How to save video to phone gallery - Dio/Flutter

I'm trying to save the video in the phone gallery (downloaded using dio package) To get the path, I'm using path provider package but unable to save it in the phone gallery
Here is my code
void downloadVideo() async {
var dir = await getExternalStorageDirectory();
final dio = Dio();
dio.download(
'video_url',
'${dir!.path}/video.mp4', // saving path, I'm trying to save it in phone gallery
onReceiveProgress: (actualBytes, totalBytes){
var percentage = actualBytes/totalBytes*100;
}
);
}
Note:- I'm aware of gallery_saver package but I need to achieve this using dio and path provider
So you already have the saving directory. You can use plugins like image_gallery_saver and gallery_saver to save your downloaded video to the gallery.
If you use image_gallery_saver, the saving code would be similar to this:
await ImageGallerySaver.saveFile(finalVideoPath);
And don't forget to delete the video in the download path after saving the video successfully to the gallery.
Final code:
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:flutter/widgets.dart';
import 'package:image_gallery_saver/image_gallery_saver.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
Future<void> downloadVideo() async {
final appDocDirectory = await getAppDocDirectory();
final finalVideoPath = join(
appDocDirectory.path,
'Video-${DateTime.now().millisecondsSinceEpoch}.mp4',
);
final dio = Dio();
await dio.download(
'video_url',
finalVideoPath,
onReceiveProgress: (actualBytes, totalBytes) {
final percentage = actualBytes / totalBytes * 100;
},
);
await saveDownloadedVideoToGallery(videoPath: finalVideoPath);
await removeDownloadedVideo(videoPath: finalVideoPath);
}
Future<Directory> getAppDocDirectory() async {
if (Platform.isIOS) {
return getApplicationDocumentsDirectory();
}
return (await getExternalStorageDirectory())!;
}
Future<void> saveDownloadedVideoToGallery({required String videoPath}) async {
await ImageGallerySaver.saveFile(videoPath);
}
Future<void> removeDownloadedVideo({required String videoPath}) async {
try {
Directory(videoPath).deleteSync(recursive: true);
} catch (error) {
debugPrint('$error');
}
}

A value of type 'XFIle' can't be assigned to a variable of type 'File' error

I am using image_picker: ^0.8.4+4 where I am getting this error. what I can do to make this code right?
late File selectedImage;
bool _isLoading = false;
CrudMethods crudMethods = CrudMethods();
Future getImage() async {
var image = await ImagePicker().pickImage(source: ImageSource.gallery);
setState(() {
selectedImage = image; //A value of type 'XFIle' can't be assigned to a variable of type 'File' error.
});
}
uploadBlog() async {
// ignore: unnecessary_null_comparison
if (selectedImage != null) {
setState(() {
_isLoading = true;
});
you can conver XFile to File using following line:
selectedImage = File(image.path);
First, you should create your variable as XFile
Because this is what you get from image picker.
XFile photo;
void _pickImage() async {
final ImagePicker _picker = ImagePicker();
photo = await _picker.pickImage(source: ImageSource.camera);
if (photo == null) return;
}
And then you can use your image as a file image.
Image.file(File(photo.path))
This happens because the package (image_picker ) you are using is relying on XFile and not File, as previously did.
So, first you have to create a variable of type File so you can work with later as you did, and after fetching the selectedImage you pass the path to instantiate the File. Like this:
File? selectedImage;
bool _isLoading = false;
CrudMethods crudMethods = CrudMethods();
Future getImage() async {
var image = await ImagePicker().pickImage(source: ImageSource.gallery);
setState(() {
selectedImage = File(image!.path); // won't have any error now
});
}
//implement the upload code
XFile and File can be converted to each other like the following in Flutter:
import 'dart:io';
import 'package:camera/camera.dart';
XFile takenPhotoXFile = await _controller!.takePicture();
// XFile to File
File photoAsFile = File(takenPhotoXFile.path);
// File to XFile
XFile imageFileAsXFile = XFile(photoAsFile.path);

Unable to upload file flutter: too many positional arguments 0 expected

Future pickImage() async {
final pickedFile = await picker.getImage(source: ImageSource.camera);
setState(() {
_imageFile = File(pickedFile.path);
});
}
Error is thrown on line File(pickedFile.path), can't compile
The issue was on package, I realized my file had
import 'package:file/file.dart';
package and
import 'dart:io';
I commented out
// import 'package:file/file.dart';
and all is fine now.

Uploading an Image to Firebase Storage with firebase_storage 4.0.0 on Flutter Web?

It looks like with the newest version(s) of Firebase Storage, the method .put(...) has been deprecated in favor of .putData(Uint8List) and .putFile(...), which I haven't found a solution for for Flutter Web yet.
The code I am trying is this, but it's not returning anything or throwing any errors.
_startFilePicker() async {
InputElement uploadInput = FileUploadInputElement();
uploadInput.click();
uploadInput.onChange.listen((e) {
// read file content as dataURL
final files = uploadInput.files;
if (files.length == 1) {
final file = files[0];
FileReader reader = FileReader();
reader.onLoadEnd.listen((e) async {
setState(() {
uploadedImage = reader.result;
});
await uploadImage();
});
reader.onError.listen((fileEvent) {});
reader.readAsArrayBuffer(file);
}
});
}
Future uploadImage() async {
StorageReference storageReference =
FirebaseStorage.instance.ref().child(userID + '/userPhoto');
try {
StorageUploadTask uploadTask = storageReference.putData(uploadedImage);
await uploadTask.onComplete;
} catch (e) {
print(e);
}
print('File Uploaded');
storageReference.getDownloadURL().then((fileURL) {
setState(() {
_formData['photo'] = fileURL;
updateUserData({'photo': fileURL});
});
});
}
Is there anything I'm doing wrong or a better way to do this?
UPDATE - 14/04/2021 - Working with firebase_core: ^1.0.2 and firebase_storage: ^8.0.3
import 'package:firebase_storage/firebase_storage.dart';
import 'package:path/path.dart';
import 'dart:io';
Future uploadProfilePhotoToFirebase(File _image) async {
String fileName = basename(_image.path); //Get File Name - Or set one
Reference firebaseStorageRef = FirebaseStorage.instance.ref().child('uploads/$fileName');
TaskSnapshot uploadTask = await firebaseStorageRef.putFile(_image);
String url = await uploadTask.ref.getDownloadURL(); //Get URL
return await membersCollection.doc(uid).update({ //Update url in Firestore (if required)
'displayPhoto': url,
});
}
OLD ANSWER
Try using the firebase package - this is working on firebase 7.3.0 which is a dependency of firebase_core 0.5.0
import 'dart:async';
import 'package:firebase/firebase.dart' as fb;
import 'dart:html' as html;
String url;
Future<String> uploadProfilePhoto(html.File image, {String imageName}) async {
try {
//Upload Profile Photo
fb.StorageReference _storage = fb.storage().ref('displayPhotos/$imageName');
fb.UploadTaskSnapshot uploadTaskSnapshot = await _storage.put(image).future;
// Wait until the file is uploaded then store the download url
var imageUri = await uploadTaskSnapshot.ref.getDownloadURL();
url = imageUri.toString();
} catch (e) {
print(e);
}
return url;
}

Create Folder When Installing Application

How to create folder in device storage to save files?
This is the code to download file into device :
import 'package:flutter_downloader/flutter_downloader.dart';
onTap: () async { //ListTile attribute
Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path;
final taskId = await FlutterDownloader.enqueue(
url: 'http://myapp/${attach[index]}',
savedDir: '/sdcard/myapp',
showNotification: true, // show download progress in status bar (for Android)
clickToOpenDownloadedFile: true, // click on notification to open downloaded file (for Android)
);
},
You can create directory when app is launched.
In the initState() method of your first screen do the logic.
Ex.
createDir() async {
Directory baseDir = await getExternalStorageDirectory(); //only for Android
// Directory baseDir = await getApplicationDocumentsDirectory(); //works for both iOS and Android
String dirToBeCreated = "<your_dir_name>";
String finalDir = join(baseDir, dirToBeCreated);
var dir = Directory(finalDir);
bool dirExists = await dir.exists();
if(!dirExists){
dir.create(/*recursive=true*/); //pass recursive as true if directory is recursive
}
//Now you can use this directory for saving file, etc.
//In case you are using external storage, make sure you have storage permissions.
}
#override
initState(){
createDir(); //call your method here
super.initState();
}
You need to import these libraries:
import 'dart:io';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
From what I saw is, you are not using appDocDir and appDocPath anywhere, cause you are saving files in /sdcard/myapp.
Please check if you are asking and granting the storage permission and also there is no way to store files in sdcard like you are doing. Either make use of predefined directories like (Document, Pictures etc.) or use device root directory that starts with storage/emulated/0
//add in pubspec.yaml
path_provider:
//import this
import 'dart:io' as io;
import 'package:path_provider/path_provider.dart';
//create Variable
String directory = (await getApplicationDocumentsDirectory()).path;
//initstate to create directory at launch time
#override
void initState() {
// TODO: implement initState
super.initState();
createFolder();
}
//call this method from init state to create folder if the folder is not exists
void createFolder() async {
if (await io.Directory(directory + "/yourDirectoryName").exists() != true) {
print("Directory not exist");
new io.Directory(directory + "/your DirectoryName").createSync(recursive: true);
//do your work
} else {
print("Directoryexist");
//do your work
}
}
Here is the Sample Codefor Creating a folder in Users internal storage Hope it Helps You
import 'dart:io' as Io;
Future _downloadImage() async {
try {
// request runtime permission
final permissionHandler = PermissionHandler();
final status = await permissionHandler
.checkPermissionStatus(PermissionGroup.storage);
if (status != PermissionStatus.granted) {
final requestRes = await permissionHandler
.requestPermissions([PermissionGroup.storage]);
if (requestRes[PermissionGroup.storage] != PermissionStatus.granted) {
_showSnackBar('Permission denined. Go to setting to granted!');
return _done();
}
}
}
var testdir =
await new Io.Directory('/storage/emulated/0/MyApp').create(recursive: true);
final filePath =
path.join(testdir.path, Filename + '.png');
print(filePath);
final file = File(filePath);
if (file.existsSync()) {
file.deleteSync();
}
//save image to storage
var request = await HttpClient().getUrl(Uri.parse(imageUrl));
var response = await request.close();
final Uint8List bytes = await consolidateHttpClientResponseBytes(response);
final saveFileResult =
saveImage({'filePath': filePath, 'bytes': bytes});
_showSnackBar(
saveFileResult
? 'Image downloaded successfully'
: 'Failed to download image',
);
} on PlatformException catch (e) {
_showSnackBar(e.message);
} catch (e, s) {
_showSnackBar('An error occurred');
debugPrint('Download image: $e, $s');
}
return _done();
}
First you need to import
1) import 'dart:io';
Second you need to create directory for the specified path in your async/await function
2) For example:
await new Directory('/storage/emulated/0/yourFolder').create(recursive: true);