From b8f7ae91ffcb77bc61f79ac8bc73e92d698df482 Mon Sep 17 00:00:00 2001 From: ASAP JAY Date: Thu, 15 Nov 2018 14:03:21 +0100 Subject: [PATCH] Code Fix, updated README, LICENSE, pubspec --- .vscode/launch.json | 13 +++++++ LICENSE | 2 +- README.md | 12 +++++- .../asapjay/thumbnails/ThumbnailsPlugin.java | 37 +++++++++++++------ example/lib/main.dart | 12 +++--- lib/validators.dart | 2 +- pubspec.yaml | 6 +-- 7 files changed, 59 insertions(+), 25 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..3287bb6 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,13 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Flutter", + "request": "launch", + "type": "dart" + } + ] +} \ No newline at end of file diff --git a/LICENSE b/LICENSE index 3b57076..758625f 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2018 Asap Jay +Copyright (c) 2018 AsapJay Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index cb6d7e0..3413ea9 100644 --- a/README.md +++ b/README.md @@ -10,10 +10,18 @@ Android OS only #### Depend on it add `thumbnails` as a dependency in your pubspec.yaml file. +#### Update Android Permissions +add the lines to AndroidManifest.xml +``` xml + + +``` +NOTE: for android versions >= 6.0 Request Permission from user at runtime before calling this plugin. + #### Add import statement Import the library ``` dart -import 'package:thumbnails/thumbnails.dart'; + import 'package:thumbnails/thumbnails.dart'; ``` #### Examples @@ -21,7 +29,7 @@ import 'package:thumbnails/thumbnails.dart'; // Fetch thumbnail, store in a specified output folder and return file path String thumb = await Thumbnails.getThumbnail( thumbOutputFile: - '[YOUR OUTPUT FILE HERE]', + '[YOUR OUTPUT FOLDER PATH TO SAVE THUMBNAILS]', // creates it if it doesnt already exist videoFile: '[VIDEO PATH HERE]', imageType: ThumbFormat.PNG, quality: 30); diff --git a/android/src/main/java/com/asapjay/thumbnails/ThumbnailsPlugin.java b/android/src/main/java/com/asapjay/thumbnails/ThumbnailsPlugin.java index e5833c0..fd40b4c 100644 --- a/android/src/main/java/com/asapjay/thumbnails/ThumbnailsPlugin.java +++ b/android/src/main/java/com/asapjay/thumbnails/ThumbnailsPlugin.java @@ -10,6 +10,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.util.Map; +import java.util.regex.Pattern; import io.flutter.plugin.common.MethodCall; import io.flutter.plugin.common.MethodChannel; @@ -66,17 +67,15 @@ private String buildThumbnail(String vidPath, String thumbPath, int type, int qu private String cacheDirectory(String vidPath, int type, int quality) { Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(vidPath, MediaStore.Video.Thumbnails.MINI_KIND); - String sourceFileName = Uri.parse(vidPath).getLastPathSegment(); - String thumbDirPath = mRegistrar.context().getExternalCacheDir() + File.separator + "ThumbFiles" + File.separator; - String tempFile = thumbDirPath + sourceFileName; - File tempDir = new File(thumbDirPath); - + String sourceFileName = getFileName(Uri.parse(vidPath).getLastPathSegment()); + File tempDir = new File(mRegistrar.context().getExternalCacheDir() + File.separator + "ThumbFiles" + File.separator); if (tempDir.exists()) { clearThumbnails(); } else { tempDir.mkdirs(); } - + String tempFile = new File(tempDir + File.separator + sourceFileName).getPath(); + System.out.println("JAVA CODE"+tempFile); switch (type) { case 1: try { @@ -113,15 +112,23 @@ private String cacheDirectory(String vidPath, int type, int quality) { } private String userDirectory(String vidPath, String thumbPath, int type, int quality) { + File fileDir = null; Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(vidPath, MediaStore.Video.Thumbnails.MINI_KIND); + String sourceFileName = getFileName(Uri.parse(vidPath).getLastPathSegment()); + fileDir = new File(thumbPath + File.separator); + if (!fileDir.exists()) { + fileDir.mkdirs(); + } + String tempFile = new File(fileDir + File.separator + sourceFileName).getAbsolutePath(); + System.out.println("ABSOLUTE JAVA CODE"+tempFile); switch (type) { case 1: try { - FileOutputStream out = new FileOutputStream(new File(thumbPath + ".jpg")); + FileOutputStream out = new FileOutputStream(new File(tempFile + ".jpg")); bitmap.compress(Bitmap.CompressFormat.JPEG, quality, out); out.flush(); out.close(); - return thumbPath + ".jpg"; + return tempFile + ".jpg"; } catch (IOException e) { e.printStackTrace(); } @@ -129,22 +136,22 @@ private String userDirectory(String vidPath, String thumbPath, int type, int qua case 2: try { - FileOutputStream out = new FileOutputStream(new File(thumbPath + ".png")); + FileOutputStream out = new FileOutputStream(new File(tempFile + ".png")); bitmap.compress(Bitmap.CompressFormat.PNG, quality, out); out.flush(); out.close(); - return thumbPath + ".png"; + return tempFile + ".png"; } catch (IOException e) { e.printStackTrace(); } break; case 3: try { - FileOutputStream out = new FileOutputStream(new File(thumbPath + "webp")); + FileOutputStream out = new FileOutputStream(new File(tempFile + "webp")); bitmap.compress(Bitmap.CompressFormat.WEBP, quality, out); out.flush(); out.close(); - return thumbPath + ".webp"; + return tempFile + ".webp"; } catch (IOException e) { e.printStackTrace(); } @@ -164,4 +171,10 @@ private void clearThumbnails() { } } } + + private static final Pattern ext = Pattern.compile("(?<=.)\\.[^.]+$"); + + private String getFileName(String s) { + return ext.matcher(s).replaceAll(""); + } } diff --git a/example/lib/main.dart b/example/lib/main.dart index b4e6e10..8b7a938 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -13,20 +13,20 @@ class _MyAppState extends State { void _buildThumbToFile() async { String thumb = await Thumbnails.getThumbnail( thumbOutputFile: - '/storage/emulated/0/Whatsapp/Media/Test${TimeOfDay.now()}.jpg', - videoFile: '/storage/emulated/0/Whatsapp/Media/.Statuses/Testvid.mp4', + '/storage/emulated/0/Videos/Thumbnails', + videoFile: '/storage/emulated/0/Videos/Testvideo.mp4', imageType: ThumbFormat.PNG, quality: 30); - print(thumb); + print('path to File: $thumb'); } -// Fetch thumbnail and stores in app temporary directory (this is volatile) +// when an output folder is not specified thumbnail are stored in app temporary directory void _buildThumbToCache() async { String thumb = await Thumbnails.getThumbnail( - videoFile: '/storage/emulated/0/Whatsapp/Media/.Statuses/Testvid.mp4', + videoFile: '/storage/emulated/0/Videos/Testvideo.mp4', imageType: ThumbFormat.JPEG, quality: 30); - print(thumb); + print('Path to cache folder $thumb'); } @override diff --git a/lib/validators.dart b/lib/validators.dart index ece8ddf..dbb4717 100644 --- a/lib/validators.dart +++ b/lib/validators.dart @@ -4,7 +4,7 @@ const DEFAULT_THUMB_QUALITY = 50; const DEFAULT_IMAGE_TYPE = ThumbFormat.JPEG; int validateQuality(int choice) { - if (choice <= 30 || choice > 100) return DEFAULT_THUMB_QUALITY; + if (choice < 10 || choice > 100 || choice == null) return DEFAULT_THUMB_QUALITY; return choice; } diff --git a/pubspec.yaml b/pubspec.yaml index 9d78da0..9c52a5e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,8 +1,8 @@ name: thumbnails -description: A new flutter plugin project. +description: A Flutter Plugin to generate thumbnails from videos.. version: 0.0.1 -author: -homepage: +author: AsapJay +homepage: https://github.com/asapJ/Flutter_Thumbnails environment: sdk: ">=2.0.0-dev.68.0 <3.0.0"