Skip to content

Commit

Permalink
External SD card fix
Browse files Browse the repository at this point in the history
  • Loading branch information
arianneorpilla committed Jul 3, 2021
1 parent 3bd79d2 commit 6b41e1e
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 82 deletions.
2 changes: 1 addition & 1 deletion android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ flutter {

dependencies {
implementation 'androidx.annotation:annotation:1.1.0'
implementation 'com.ichi2.anki:api:1.1.0alpha6'
implementation 'com.github.ankidroid:Anki-Android:api-v1.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,10 +374,21 @@ public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
}
break;
case "addMediaFromUri":
//Uri fileUri = Uri.parse(fileUriPath);
//String addedFileName = api.addMediaFromUri(fileUri, preferredName, mimeType);
//result.success(addedFileName);
//break;
System.out.println(fileUriPath);
System.out.println(preferredName);
System.out.println(mimeType);
Uri fileUri = Uri.parse(fileUriPath);

try {
String addedFileName = api.addMediaFromUri(fileUri, preferredName, mimeType);
result.success(addedFileName);
System.out.println("Added media from URI");
} catch (Exception e) {
System.out.println(e);
}


break;
default:
result.notImplemented();
}
Expand Down
2 changes: 2 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ buildscript {
repositories {
google()
jcenter()

}

dependencies {
Expand All @@ -15,6 +16,7 @@ allprojects {
repositories {
google()
jcenter()
maven { url "https://jitpack.io" }
}
}

Expand Down
Binary file added flutter_02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
78 changes: 51 additions & 27 deletions lib/anki.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,20 @@ Future<void> requestAnkiDroidPermissions() async {
await platform.invokeMethod('requestPermissions');
}

Directory getDCIMDirectory() {
return Directory("storage/emulated/0/DCIM/jidoujisho/");
}

String getPreviewImagePath() {
return "$gAppDirPath/exportImage.jpg";
return getDCIMDirectory().path + "exportImage.jpg";
}

String getPreviewImageMultiPath(int index) {
return "$gAppDirPath/exportMulti$index.jpg";
return getDCIMDirectory().path + "exportMulti$index.jpg";
}

String getPreviewAudioPath() {
return "$gAppDirPath/exportAudio.mp3";
return getDCIMDirectory().path + "exportAudio.mp3";
}

Future exportCurrentFrame(
Expand Down Expand Up @@ -128,6 +132,7 @@ Future exportCurrentAudio(
int subtitleDelay,
) async {
File audioFile = File(getPreviewAudioPath());
String previewAudioPath = audioFile.path;
if (audioFile.existsSync()) {
audioFile.deleteSync();
}
Expand Down Expand Up @@ -170,7 +175,7 @@ Future exportCurrentAudio(
break;
}

String outputPath = "\"$gAppDirPath/exportAudio.mp3\"";
String outputPath = "\"$previewAudioPath\"";
String command =
"-loglevel verbose -ss $timeStart -to $timeEnd -y -i \"$inputPath\" -map 0:a:$audioIndex $outputPath";

Expand Down Expand Up @@ -672,8 +677,29 @@ class _DeckDropDownState extends State<DeckDropDown> {
}
}

Future<String> addMediaFromUri(
String fileUriPath,
String preferredName,
String mimeType,
) async {
const platform = const MethodChannel('com.lrorpilla.api/ankidroid');

try {
return await platform.invokeMethod('addMediaFromUri', <String, dynamic>{
'fileUriPath': fileUriPath,
'preferredName': preferredName,
'mimeType': mimeType,
});
} on PlatformException catch (e) {
print("Failed to add media from URI");
print(e);
}

return null;
}

void exportAnkiCard(String deck, String sentence, String answer, String reading,
String meaning, bool isSingle, int selectedIndex) {
String meaning, bool isSingle, int selectedIndex) async {
DateTime now = DateTime.now();
String newFileName =
"jidoujisho-" + intl.DateFormat('yyyyMMddTkkmmss').format(now);
Expand All @@ -687,25 +713,27 @@ void exportAnkiCard(String deck, String sentence, String answer, String reading,

File audioFile = File(getPreviewAudioPath());

String newImagePath = path.join(
getAnkiDroidDirectory().path,
"collection.media/$newFileName.jpg",
);
String newAudioPath = path.join(
getAnkiDroidDirectory().path,
"collection.media/$newFileName.mp3",
);
// String newImagePath = path.join(
// getAnkiDroidDirectory().path,
// "collection.media/$newFileName.jpg",
// );
// String newAudioPath = path.join(
// getAnkiDroidDirectory().path,
// "collection.media/$newFileName.mp3",
// );

String addImage = "";
String addAudio = "";

if (imageFile.existsSync()) {
imageFile.copySync(newImagePath);
addImage = "<img src=\"$newFileName.jpg\">";
if (imageFile != null && imageFile.existsSync()) {
addImage = await addMediaFromUri(
"file:///" + imageFile.uri.toString(), newFileName, "image");
print("IMAGE FILE EXPORTED: $addImage");
}
if (audioFile.existsSync()) {
audioFile.copySync(newAudioPath);
addAudio = "[sound:$newFileName.mp3]";
if (audioFile != null && audioFile.existsSync()) {
addAudio = await addMediaFromUri(
"file:///" + audioFile.uri.toString(), newFileName, "audio");
print("AUDIO FILE EXPORTED: $addAudio");
}

if (answer == "") {
Expand All @@ -726,22 +754,18 @@ void exportAnkiCard(String deck, String sentence, String answer, String reading,
}

void exportCreatorAnkiCard(String deck, String sentence, String answer,
String reading, String meaning, File imageFile, bool isReader) {
String reading, String meaning, File imageFile, bool isReader) async {
DateTime now = DateTime.now();
String newFileName =
"jidoujisho-" + intl.DateFormat('yyyyMMddTkkmmss').format(now);

String newImagePath = path.join(
getAnkiDroidDirectory().path,
"collection.media/$newFileName.jpg",
);

String addImage = "";
String addAudio = "";

if (imageFile != null && imageFile.existsSync()) {
imageFile.copySync(newImagePath);
addImage = "<img src=\"$newFileName.jpg\">";
addImage = await addMediaFromUri(
"file:///" + imageFile.uri.toString(), newFileName, "image");
print("IMAGE FILE EXPORTED: $addImage");
}

if (answer == "") {
Expand Down
55 changes: 6 additions & 49 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void main() async {
gAppDirPath = (await getApplicationDocumentsDirectory()).path;
gPackageInfo = await PackageInfo.fromPlatform();
gMecabTagger = Mecab();
gMecabTagger.init("assets/ipadic", true);
await gMecabTagger.init("assets/ipadic", true);

if (Platform.isAndroid) {
AndroidDeviceInfo androidInfo = await DeviceInfoPlugin().androidInfo;
Expand All @@ -71,6 +71,10 @@ void main() async {
}
}

if (!getDCIMDirectory().existsSync()) {
getDCIMDirectory().createSync(recursive: true);
}

gSharedPrefs = await SharedPreferences.getInstance();
gIsSelectMode = ValueNotifier<bool>(getSelectMode());
bool canResume;
Expand Down Expand Up @@ -1720,10 +1724,6 @@ class _HomeState extends State<Home> {
child: const Text('Report a bug or problem'),
value: 'Report a bug or problem',
),
PopupMenuItem<String>(
child: const Text('Set AnkiDroid directory'),
value: 'Set AnkiDroid directory',
),
PopupMenuItem<String>(
child: const Text('About this app'),
value: 'About this app',
Expand Down Expand Up @@ -1849,49 +1849,6 @@ class _HomeState extends State<Home> {
case "Report a bug or problem":
await launch("https://github.com/lrorpilla/jidoujisho/issues/new");
break;
case "Set AnkiDroid directory":
String currentDirectoryPath = getAnkiDroidDirectory().path;
TextEditingController _textFieldController = TextEditingController(
text: currentDirectoryPath,
);

showDialog(
context: context,
builder: (context) {
return AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.zero,
),
content: TextField(
controller: _textFieldController,
decoration: InputDecoration(
hintText: "storage/emulated/0/AnkiDroid",
labelText: 'AnkiDroid directory path'),
),
actions: <Widget>[
TextButton(
child: Text('CANCEL', style: TextStyle(color: Colors.white)),
onPressed: () {
Navigator.pop(context);
},
),
TextButton(
child: Text('OK', style: TextStyle(color: Colors.white)),
onPressed: () async {
String newDirectoryPath = _textFieldController.text;
Directory newDirectory = Directory(newDirectoryPath);

if (newDirectory.existsSync()) {
await setAnkiDroidDirectory(newDirectory);
Navigator.pop(context);
}
},
),
],
);
},
);
break;
case "About this app":
const String legalese = "A mobile video player, reader assistant and card creation toolkit tailored for language learners.\n\n" +
"Built for the Japanese language learning community by Leo Rafael Orpilla. " +
Expand Down Expand Up @@ -4384,7 +4341,7 @@ class _CreatorState extends State<Creator> {
crossAxisAlignment: WrapCrossAlignment.end,
children: [
Text(
"Preparing card creator",
"Preparing card creator...",
style: TextStyle(
color: Colors.grey,
fontSize: 20,
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: jidoujisho
description: A mobile video player tailored for Japanese language learners.
publish_to: none

version: 0.23.1+55
version: 0.23.2+56

environment:
sdk: ">=2.7.0 <3.0.0"
Expand Down

0 comments on commit 6b41e1e

Please sign in to comment.