I'm new in flutter.
Hope my issue below will be same your issue in the past. And you can help me for this:
My situation is:
My app will capture an photo (using package camera: ^0.5.8+2). Then, this photo will be saved on gallery of user device (using package gallery_saver: ^2.0.1). Finally, I will open the gallery (using package photo_manager: ^0.5.7) to pick some photos which I captured before ==> This flow is really clear and simple, right? ^_^
But, I have already trap at this issue:
After I captured an photo, then open the gallery from my app, I can't see this photo (Althought I can see this photo on device gallery). I also print the log on console to make sure that: my recently photo was got by my app. But, I still see nothing, it's mean my app also can't read this recently photo.
So, what do I wrong? Or what am I missing? Everyone who have experience about this issue help me, pls!
Below is my source code:
This is function to take photo by using camera package (https://pub.dev/packages/camera)
/// Take an photo
_takePhoto() async {
try {
if (_numberPhotoTaken < widget.maxTaken) {
/// Path
var file = "${DateTime.now().toString()}.jpg";
var temporaryDir = (await getTemporaryDirectory()).path;
var pathSaved = join(temporaryDir, file);
/// Take photo
await _cameraController.takePicture(pathSaved);
/// save to gallery
GallerySaver.saveImage(pathSaved, albumName: galleryFolder).then((ok) {
if (ok) {
setState(() => _numberPhotoTaken++);
}
/// remove temporary file
File(pathSaved).delete();
});
} else {
AppDialog.inform(
this.context,
textLocale("Reached the maximum number of photos", this.context),
() {});
}
} on CameraException catch (e) {
} on Exception catch (ex) {
}
}
This is function to load asset from gallery by using photo manager package (https://pub.dev/packages/photo_manager)
/// fetching gallery's asset
/// return: list of asset entity in specific page
Future<List<GalleryEntity>> _fetchGalleryPaging() async {
/// load all assets in one album list
List<AssetPathEntity> albums = await PhotoManager.getAssetPathList(
onlyAll: true, type: RequestType.common);
List<AssetEntity> page = await albums[0].getAssetListPaged(_currentPage, widget.perPage);
/// return gallery entity list
return page.map<GalleryEntity>((e) => GalleryEntity(asset: e)).toList();
}
I'm currently test on my android device
When I open gallery on device, I wonder: the new photo which I have just captured not be arranged right (the newest photo must be at the end of gallery). After a few time (I'm not sure how many time), device gallery will auto refresh, and my recently photo will be arranged in the right way (it's at the end of gallery). And my app will read this photo perfectly (so strange for me)
UPDATE: one more thing, my photo will be saved into a separated folder in gallery
If you read till this line. I'm so appreciate about that. And I hope among of you have experience to solve this issue
Thanks all guys,
After a few time I made searching about my issue. I find out the main problem is the external storage was not rescan when I did an capturing photo, so my flutter app can not be display the new photo on gallery.
The solution for this issue is the package:
https://github.com/hui-z/image_gallery_saver
I'm not sure about the effective of this package, but for my scene, it's work as perfect as I wish.
Hope this help for anyone who also face same issue like me
Thanks all
Related
I have a problem using image_picker on flutter in real device with android. When i open gallery using ImageSource.gallery the first screen is "Recent pictures", i can select any picture and everything fine, but if i open the gallery i see "Lost device connection" message and white screen.
_procesImage(ImageSource image) async {
final ImagePicker picker = ImagePicker();
file = await picker.pickImage(source: image);
setState(() {});
}
I beleive you're having trouble with iOS as you're talking about gallery but could you give more detail ? Is it happening on emulator or real device?
I would suggest you to doublecheck iOS specific authorizations in plist, also check if you're asking for storage permission before opening the gallery.
I had that kind of issue month ago but my library wasn't up to date and I guess updating it fixed the issue
Try to define methods like these and specifically point out from which location you want to access the image. Currently, it is not specified.
final ImagePicker imagePicker = ImagePicker();
_imgFromCamera() async {
final image = await imagePicker.pickImage(
source: ImageSource.camera);
// the further functionality here
sendImageToServer(image);
}
_imgFromGallery() async {
final image = await imagePicker.pickImage(
source: ImageSource.gallery);
// the further functionality here
sendImageToServer(image);
}
I'm trying to pick multiple images at once using the image_picker 0.7.5+3 package
so my code is simple , just one line to open the gallery to select from it
final images = await ImagePicker.platform.pickMultiImage();
but it's showing this error
enter image description here
i tried running flutter clean and deleting the app from emulator and retarting everything but it's the same problem,
note : using final images = await ImagePicker().getImage(source: ImageSource.gallery); it allows me to choose an image without any problems
final result = await ImagePicker().pickMultiImage(
imageQuality: 70,
maxWidth: 1440,
);
This should work as usual. If you are testing on android emulator and the default file manager, you have to long press the first image to multi-select.
Try a long click on one image, then it will let you pick multiple images.
I can't find anything in their API reference for pickMultiImage. I don't think it exists, unfortunately. There's a package called images_picker, maybe that helps you.
I'm doing a bit of feasability R&D on a project I've been thinking about, the problem is that I'm unfamiliar with the limitations of the Camera API or the OS itself.
I want to write a cordova app which when opened (and authorized), will take a picture every nth second and upload it to a FTP site. Consider it something like a CCTV function, where my site will continuously render the latest image to the user.
Some pseudo code:
while (true) {
var img = api.takePicture(args);
uploadImage(img);
thread.sleep(1000);
}
So my question is, can I access the camera and instruct it to take a picture without user intervention (again, after camera access is authorized)?
Examples or direction for any API call to accomplish it would be really appreciated. I saw this article, and the answer looks promising, but the OP has android and I'd to know if it behaves the same on iOS.
On a side note, how do I test my cordova application on my iPhone without buying an app store license? I only want to run it on my own device.
Solved using CameraPictureBackground plugin:
function success(imgurl) { console.log("Imgurl = " + imgurl); //here I added my function to upload the saved pictures //on my internet server using file-tranfer plugin }
function onFail(message) {
alert('Failed because: ' + message); }
function CaptureBCK() {
var options = {
name: "Image", //image suffix
dirName: "CameraPictureBackground", //foldername
orientation: "portrait", //or landscape
type: "back" //or front
};
window.plugins.CameraPictureBackground.takePicture(success, onFail, options); }
<button onclick="CaptureBCK();">Capture Photo</button> <br>
You will find your pictures under CameraPictureBackground directory in your device acrhive. I used also file-transfer plugin in order to directly upload the pictures on my server over the internet.
I did a PhoneGap Android app that starts the camera, a picture is taken and saved to sdcard. The app shows the picture on the initial screen. The app also allows to navigate through some additional web pages. If I navigate away (to another web page) and come back to the first screen, the picture is no longer there. I tried to get it from the sdcard and show it using the HTML tag, to be seen even before another picture is taken, but the picture is not shown (I see the "alt" text instead).
I am not sure whether my problem is clear, but I am ready to answer your questions to clarify it.
My project is in the GitHub at the address https://github.com/monicamarcus/Android_PhoneGap
Anyone can help me? Thanks!
The Javascript that shows the picture the first time:
//Called when a photo is successfully retrieved
//
function onPhotoDataSuccess(imageData) {
var smallImage = document.getElementById('cameraPic');
smallImage.style.display = 'block'
smallImage.src = "data:image/jpeg;base64," + imageData;
}
Try this:
function onPhotoDataSuccess(imageData) {
localStorage.img="data:image/jpeg;base64,"+imageData;
var smallImage = document.getElementById('cameraPic');
smallImage.src= localStorage.img;
smallImage.style.display = 'block';
}
document.addEventListener('deviceready',function(){
if(typeof(localStorage.img)!='undefined'){
smallImage=document.getElementById('cameraPic');
smallImage.src=localStorage.img;
smallImage.style.display='block';
}
},false);
I have used an answer from here
How should I give images rounded corners in Android?
However when created the picture with rounded edges it is being saved in my camera gallery as well as in a custom location that I have specified, any reason why this would happen or anyway to get rid of it automatically?
I don't want a picture being saved here.
Images are not saved in your device gallery - the gallery just displays all the images that are on your phone, UNLESS they are in storage associated with particular application. Use the following method do determine where you should save the images.
private File getAbosoluteFile(String ralativePath, Context context) {
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
return new File(context.getExternalFilesDir(null), ralativePath);
} else {
return new File(context.getFilesDir(), ralativePath);
}
}
It will store them in external storage associated with your application and they will seize being displayed in the device's gallery. Try this out with new image, because images are cached for a long time in the gallery application.