91 lines
2.5 KiB
Java
91 lines
2.5 KiB
Java
package com.github.catvod.utils;
|
|
|
|
import android.content.Intent;
|
|
import android.net.Uri;
|
|
import android.os.Build;
|
|
import android.text.TextUtils;
|
|
|
|
import com.github.catvod.spider.Init;
|
|
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.net.URLConnection;
|
|
|
|
public class FileUtil {
|
|
|
|
public static File getCacheDir() {
|
|
return Init.context().getCacheDir();
|
|
}
|
|
|
|
public static File getCacheFile(String fileName) {
|
|
return new File(getCacheDir(), fileName);
|
|
}
|
|
|
|
public static void write(File file, String data) {
|
|
write(file, data.getBytes());
|
|
}
|
|
|
|
public static void write(File file, byte[] data) {
|
|
try {
|
|
FileOutputStream fos = new FileOutputStream(file);
|
|
fos.write(data);
|
|
fos.flush();
|
|
fos.close();
|
|
chmod(file);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public static File chmod(File file) {
|
|
try {
|
|
Process process = Runtime.getRuntime().exec("chmod 777 " + file);
|
|
process.waitFor();
|
|
return file;
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
return file;
|
|
}
|
|
}
|
|
|
|
public static String read(File file) {
|
|
try {
|
|
return read(new FileInputStream(file));
|
|
} catch (Exception e) {
|
|
return "";
|
|
}
|
|
}
|
|
|
|
public static String read(InputStream is) {
|
|
try {
|
|
byte[] data = new byte[is.available()];
|
|
is.read(data);
|
|
is.close();
|
|
return new String(data, "UTF-8");
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
return "";
|
|
}
|
|
}
|
|
|
|
public static void openFile(File file) {
|
|
Intent intent = new Intent(Intent.ACTION_VIEW);
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
|
intent.setDataAndType(getShareUri(file), FileUtil.getMimeType(file.getName()));
|
|
Init.context().startActivity(intent);
|
|
}
|
|
|
|
private static String getMimeType(String fileName) {
|
|
String mimeType = URLConnection.guessContentTypeFromName(fileName);
|
|
return TextUtils.isEmpty(mimeType) ? "*/*" : mimeType;
|
|
}
|
|
|
|
private static Uri getShareUri(File file) {
|
|
return Build.VERSION.SDK_INT < Build.VERSION_CODES.N ? Uri.fromFile(file) : FileProvider.getUriForFile(Init.context(), Init.context().getPackageName() + ".provider", file);
|
|
}
|
|
}
|