Skip to content

Commit

Permalink
Code Fix, updated README, LICENSE, pubspec
Browse files Browse the repository at this point in the history
  • Loading branch information
asapJ committed Nov 15, 2018
1 parent 191744b commit b8f7ae9
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 25 deletions.
13 changes: 13 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,26 @@ 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
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
```
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
``` 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);
Expand Down
37 changes: 25 additions & 12 deletions android/src/main/java/com/asapjay/thumbnails/ThumbnailsPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -113,38 +112,46 @@ 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();
}
break;

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();
}
Expand All @@ -164,4 +171,10 @@ private void clearThumbnails() {
}
}
}

private static final Pattern ext = Pattern.compile("(?<=.)\\.[^.]+$");

private String getFileName(String s) {
return ext.matcher(s).replaceAll("");
}
}
12 changes: 6 additions & 6 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ class _MyAppState extends State<MyApp> {
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
Expand Down
2 changes: 1 addition & 1 deletion lib/validators.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
6 changes: 3 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
homepage: https://github.com/asapJ/Flutter_Thumbnails

environment:
sdk: ">=2.0.0-dev.68.0 <3.0.0"
Expand Down

0 comments on commit b8f7ae9

Please sign in to comment.