Update LyricUtil.java

This commit is contained in:
tva2000hn 2019-09-24 20:02:04 +07:00 committed by GitHub
parent 608f5ca35a
commit 8a8c2a8b88
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -15,6 +15,7 @@
package code.name.monkey.retromusic.util; package code.name.monkey.retromusic.util;
import android.util.Base64; import android.util.Base64;
import android.util.Log;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
@ -40,11 +41,11 @@ public class LyricUtil {
public static File writeLrcToLoc(@NonNull String title, @NonNull String artist, @NonNull String lrcContext) { public static File writeLrcToLoc(@NonNull String title, @NonNull String artist, @NonNull String lrcContext) {
FileWriter writer = null; FileWriter writer = null;
try { try {
File file = new File(getLrcPath(title, artist)); File file = new File(getLrcPath2(title, artist));
if (!file.getParentFile().exists()) { if (!file.exists()) {
file.getParentFile().mkdirs(); file.mkdirs();
} }
writer = new FileWriter(getLrcPath(title, artist)); writer = new FileWriter(getLrcPath2(title, artist));
writer.write(lrcContext); writer.write(lrcContext);
return file; return file;
} catch (IOException e) { } catch (IOException e) {
@ -70,17 +71,41 @@ public class LyricUtil {
return file.exists(); return file.exists();
} }
@NonNull public static boolean isLrcFile2Exist(@NonNull String title, @NonNull String artist) {
public static File getLocalLyricFile(@NonNull String title, @NonNull String artist) { File file = new File(getLrcPath2(title, artist));
File file = new File(getLrcPath(title, artist));
if (file.exists()) { return file.exists();
return file;
} else {
return new File("lyric file not exist");
}
} }
private static String getLrcPath(String title, String artist) { @NonNull
public static File getLocalLyricFile(@NonNull String title, @NonNull String artist) {
try{
File file = new File(getLrcPath(title, artist));
File file2 = new File(getLrcPath2(title, artist));
if (file.exists()) {
return file;
} else if (file2.exists()) {
return file2;
}
else {
return new File("lyric file not exist");
}} catch (Exception dfs){
dfs.printStackTrace();
return new File("lyric file not exist");
}
}
public static String getLrcPath2(String title, String artist) {
int fileExtPos = title.lastIndexOf(".");
String x2 = title.substring(0, title.length() -4 ) + ".lrc";
Log.d("damn2",x2);
return x2;
}
public static String getLrcPath(String title, String artist) {
return lrcRootPath + title + " - " + artist + ".lrc"; return lrcRootPath + title + " - " + artist + ".lrc";
} }
@ -103,10 +128,18 @@ public class LyricUtil {
@NonNull @NonNull
public static String getStringFromFile(@NonNull String title, @NonNull String artist) throws Exception { public static String getStringFromFile(@NonNull String title, @NonNull String artist) throws Exception {
File file = new File(getLrcPath(title, artist)); File file;
File file2 = new File(getLrcPath(title, artist));
File file3 = new File(getLrcPath2(title, artist));
if(file2.exists()){
file = file2;
} else {
file = file3;
}
FileInputStream fin = new FileInputStream(file); FileInputStream fin = new FileInputStream(file);
String ret = convertStreamToString(fin); String ret = convertStreamToString(fin);
fin.close(); fin.close();
// Log.d("damn2",ret);
return ret; return ret;
} }