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

[How to use] (PHPhotosErrorDomain error -1.), No failure reason provided, null) #1258

Open
matifdeveloper opened this issue Jan 20, 2025 · 1 comment

Comments

@matifdeveloper
Copy link

matifdeveloper commented Jan 20, 2025

Platforms

iOS

Description

I am trying to convert mp4 video to LivePhoto from url

Error: PlatformException(PHPhotosErrorDomain (-1), The operation couldn’t be completed. (PHPhotosErrorDomain error -1.), No failure reason provided, null)

My code

import 'dart:io';
import 'package:flutter/services.dart';
import 'package:http/http.dart' as http;
import 'package:material_wallpapers_ios/sources.dart';
import 'package:path_provider/path_provider.dart';
import 'package:photo_manager/photo_manager.dart';
import 'package:video_thumbnail/video_thumbnail.dart';

mixin LivePhotoManager {
  // Downloads a file from a URL and returns the file.
  static Future<File> downloadFile(String url, String fileName) async {
    try {
      final response = await http.get(Uri.parse(url));
      if (response.statusCode != 200) {
        throw Exception('Failed to download file: ${response.statusCode}');
      }

      final bytes = response.bodyBytes;
      final dir = await getTemporaryDirectory();
      final file = File('${dir.path}/$fileName');
      await file.writeAsBytes(bytes);

      return file;
    } catch (e) {
      FlutterLogger.error('Failed to download file: $e');
      rethrow;
    }
  }

  // Main function to create a live photo from an image and video URL.
  static Future<bool> createLivePhoto(String imageUrl, String videoUrl) async {
    try {
      // Step 1: Download image and video
      final imageFile = await downloadFile(imageUrl, 'live_photo.heic');
      final videoFile = await downloadFile(videoUrl, 'live_video.mp4');

      // Step 2: Validate the files exist and are of the correct types
      if (!(await imageFile.exists()) || !(await videoFile.exists())) {
        throw Exception('One or both files do not exist or are invalid.');
      }

      // Step 3: Check that the image is in HEIC format
      if (!imageFile.path.endsWith('.heic')) {
        throw Exception('The image file must be in HEIC format.');
      }

      // Step 4: Check that the video is in MP4 format
      if (!videoFile.path.endsWith('.mp4')) {
        throw Exception('The video file must be in MP4 format.');
      }

      FlutterLogger.info(imageFile.path);
      FlutterLogger.info(videoFile.path);

      // Step 5: Save the live photo
      final AssetEntity entity = await PhotoManager.editor.darwin.saveLivePhoto(
        imageFile: imageFile,
        videoFile: videoFile,
        title: 'LivePhoto_Example.heic',
      );

      FlutterLogger.success('Live photo saved successfully: ${entity.id}');
      return true;
    } catch (e) {
      // Handle the error and provide better logs.
      FlutterLogger.error('Error creating live photo: $e');
      if (e is PlatformException) {
        // Handle platform-specific errors (e.g., permission issues, file access issues)
        FlutterLogger.error('Platform-specific error: ${e.message}');
        FlutterLogger.error('Error code: ${e.code}');
      }
      return false;
    }
  }
}

Try do it

No response

@fluttercandies-dev
Copy link

AI Summary: The user is encountering a PHPhotosErrorDomain error -1 when trying to create a Live Photo from an MP4 video on iOS. The code downloads an image and video, then attempts to save them as a Live Photo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants