98 lines
3.0 KiB
Java
98 lines
3.0 KiB
Java
package com.live.mylivewallpaper.help;
|
|
|
|
import android.content.Context;
|
|
import android.net.Uri;
|
|
import android.util.DisplayMetrics;
|
|
import android.util.Log;
|
|
|
|
import androidx.core.content.FileProvider;
|
|
|
|
import com.live.mylivewallpaper.App;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.io.File;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.InputStreamReader;
|
|
import java.io.Reader;
|
|
import java.io.StringWriter;
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
public class Common {
|
|
private static String TAG = "=============";
|
|
|
|
public static String getCovertStr(InputStream stream) {
|
|
String covertStr = "";
|
|
try {
|
|
StringWriter writer = new StringWriter();
|
|
char[] buffer = new char[stream.available()];
|
|
Reader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
|
|
int a = 0;
|
|
while ((a = reader.read(buffer)) != -1) {
|
|
writer.write(buffer, 0, a);
|
|
}
|
|
covertStr = writer.toString();
|
|
} catch (IOException e) {
|
|
return covertStr;
|
|
}
|
|
return covertStr;
|
|
}
|
|
|
|
public static void logMsg(String msg) {
|
|
Log.d(TAG, msg);
|
|
}
|
|
|
|
public static int dpToPx(Context context,int dp) {
|
|
return Math.round(dp * (context.getResources().getDisplayMetrics().xdpi / DisplayMetrics.DENSITY_DEFAULT));
|
|
}
|
|
|
|
public static int pxToDp(Context context,int px) {
|
|
return Math.round(px / (context.getResources().getDisplayMetrics().xdpi / DisplayMetrics.DENSITY_DEFAULT));
|
|
}
|
|
|
|
|
|
|
|
public static boolean writeFile(InputStream input, String filePath) {
|
|
try {
|
|
byte[] byteArray = new byte[4096];
|
|
ByteArrayOutputStream output = new ByteArrayOutputStream();
|
|
int bytesRead;
|
|
while ((bytesRead = input.read(byteArray)) != -1) {
|
|
output.write(byteArray, 0, bytesRead);
|
|
}
|
|
|
|
File file = new File(filePath);
|
|
if (!file.exists()) {
|
|
file.createNewFile();
|
|
}
|
|
|
|
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
|
|
fileOutputStream.write(output.toByteArray());
|
|
output.close();
|
|
fileOutputStream.close();
|
|
Common.logMsg("-------------onSuccess return true");
|
|
return true;
|
|
} catch (Exception ex) {
|
|
Log.d("-----------", "---------ex=" + ex.getMessage());
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static String getCachePath(int id){
|
|
App application = App.getApplication();
|
|
String s1 = application.getCacheDir() +"/"+ id+".mp4";
|
|
return s1;
|
|
|
|
}
|
|
|
|
|
|
public static Uri getUriFromFilePath(Context context, String filePath) {
|
|
File file = new File(filePath);
|
|
|
|
// 使用 FileProvider 获取 URI
|
|
return FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", file);
|
|
}
|
|
}
|