269 lines
9.6 KiB
Java
269 lines
9.6 KiB
Java
package com.uiui.zyos.utils;
|
|
|
|
import android.content.ContentResolver;
|
|
import android.content.Context;
|
|
import android.database.Cursor;
|
|
import android.graphics.Bitmap;
|
|
import android.graphics.BitmapFactory;
|
|
import android.net.Uri;
|
|
import android.os.Environment;
|
|
import android.provider.MediaStore;
|
|
import android.text.TextUtils;
|
|
import android.util.Log;
|
|
|
|
import androidx.core.content.ContextCompat;
|
|
|
|
import com.arialyy.aria.core.Aria;
|
|
import com.uiui.zyos.bean.AppUpdateInfo;
|
|
import com.uiui.zyos.bean.AriaDownloadInfo;
|
|
import com.uiui.zyos.gson.GsonUtils;
|
|
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.FileOutputStream;
|
|
import java.math.BigInteger;
|
|
import java.security.MessageDigest;
|
|
import java.util.HashSet;
|
|
|
|
public class FileUtil {
|
|
private static final String TAG = "FileUtil";
|
|
|
|
public static String getFileType(String url) {
|
|
if (url.indexOf("/") == -1) {
|
|
return url.substring(url.indexOf("."), url.length());
|
|
} else {
|
|
String fileName = url.substring(url.lastIndexOf("/"));
|
|
return fileName.substring(fileName.indexOf("."), fileName.length());
|
|
}
|
|
}
|
|
|
|
public static boolean isLocalPath(String path) {
|
|
return path.startsWith(File.separator);
|
|
}
|
|
|
|
private static HashSet<String> videoFormat = new HashSet<String>() {{
|
|
this.add(".mp4");
|
|
this.add(".avi");
|
|
this.add(".nkv");
|
|
this.add(".flv");
|
|
}};
|
|
private static HashSet<String> pictureFormat = new HashSet<String>() {{
|
|
this.add(".png");
|
|
this.add(".jpg");
|
|
this.add(".jpeg");
|
|
this.add(".bmp");
|
|
}};
|
|
|
|
public static boolean isVideoFile(String fileName) {
|
|
if (TextUtils.isEmpty(fileName)) {
|
|
return false;
|
|
} else {
|
|
if (!fileName.startsWith(".")) {
|
|
return videoFormat.contains(getFileType(fileName));
|
|
} else {
|
|
return videoFormat.contains(fileName);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static boolean isPictureFile(String fileName) {
|
|
if (TextUtils.isEmpty(fileName)) {
|
|
return false;
|
|
} else {
|
|
if (!fileName.startsWith(".")) {
|
|
return pictureFormat.contains(getFileType(fileName));
|
|
} else {
|
|
return pictureFormat.contains(fileName);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static File uriToFile(Uri uri, Context context) {
|
|
String path = null;
|
|
if ("file".equals(uri.getScheme())) {
|
|
path = uri.getEncodedPath();
|
|
if (path != null) {
|
|
path = Uri.decode(path);
|
|
ContentResolver cr = context.getContentResolver();
|
|
StringBuffer buff = new StringBuffer();
|
|
buff.append("(").append(MediaStore.Images.ImageColumns.DATA).append("=").append("'" + path + "'").append(")");
|
|
Cursor cur = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.DATA}, buff.toString(), null, null);
|
|
int index = 0;
|
|
int dataIdx = 0;
|
|
for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
|
|
index = cur.getColumnIndex(MediaStore.Images.ImageColumns._ID);
|
|
index = cur.getInt(index);
|
|
dataIdx = cur.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
|
|
path = cur.getString(dataIdx);
|
|
}
|
|
cur.close();
|
|
if (index == 0) {
|
|
} else {
|
|
Uri u = Uri.parse("content://media/external/images/media/" + index);
|
|
System.out.println("temp uri is :" + u);
|
|
}
|
|
}
|
|
if (path != null) {
|
|
return new File(path);
|
|
}
|
|
} else if ("content".equals(uri.getScheme())) {
|
|
// 4.2.2以后
|
|
String[] proj = {MediaStore.Images.Media.DATA};
|
|
Cursor cursor = context.getContentResolver().query(uri, proj, null, null, null);
|
|
if (cursor.moveToFirst()) {
|
|
int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
|
|
path = cursor.getString(columnIndex);
|
|
}
|
|
cursor.close();
|
|
|
|
return new File(path);
|
|
} else {
|
|
//Log.i(TAG, "Uri Scheme:" + uri.getScheme());
|
|
return new File(uri.toString());
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* drawable转为file
|
|
*
|
|
* @param drawableId drawable的ID
|
|
* @param fileName 转换后的文件名
|
|
* @return
|
|
*/
|
|
public static File drawableToFile(Context context, int drawableId, String fileName) {
|
|
// InputStream is = view.getContext().getResources().openRawResource(R.drawable.logo);
|
|
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), drawableId);
|
|
// Bitmap bitmap = BitmapFactory.decodeStream(is);
|
|
String defaultPath = context.getFilesDir().getAbsolutePath() + "/defaultGoodInfo";
|
|
File file = new File(defaultPath);
|
|
if (!file.exists()) {
|
|
file.mkdirs();
|
|
}
|
|
String defaultImgPath = defaultPath + "/" + fileName;
|
|
file = new File(defaultImgPath);
|
|
try {
|
|
file.createNewFile();
|
|
FileOutputStream fOut = new FileOutputStream(file);
|
|
bitmap.compress(Bitmap.CompressFormat.PNG, 20, fOut);
|
|
// is.close();
|
|
fOut.flush();
|
|
fOut.close();
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
return file;
|
|
}
|
|
|
|
/**
|
|
* bitmap
|
|
*
|
|
* @param bitmap bitmap
|
|
* @param fileName 转换后的文件名
|
|
* @return
|
|
*/
|
|
public static File bitmapToFile(Context context, Bitmap bitmap, String fileName) {
|
|
String defaultPath = context.getFilesDir().getAbsolutePath() + "/defaultGoodInfo";
|
|
File file = new File(defaultPath);
|
|
if (!file.exists()) {
|
|
file.mkdirs();
|
|
}
|
|
String defaultImgPath = defaultPath + "/" + fileName;
|
|
file = new File(defaultImgPath);
|
|
try {
|
|
file.createNewFile();
|
|
FileOutputStream fOut = new FileOutputStream(file);
|
|
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
|
|
// is.close();
|
|
fOut.flush();
|
|
fOut.close();
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
return file;
|
|
}
|
|
|
|
|
|
public static String getFileNamefromURL(String url) {
|
|
int position = url.lastIndexOf("/");
|
|
return url.substring(position + 1);
|
|
}
|
|
|
|
/**
|
|
* 获取单个文件的MD5值
|
|
*
|
|
* @param file 文件
|
|
* @return
|
|
*/
|
|
|
|
public static String getFileMd5(File file) {
|
|
if (!file.isFile()) {
|
|
return "";
|
|
}
|
|
MessageDigest digest = null;
|
|
FileInputStream in = null;
|
|
byte buffer[] = new byte[1024];
|
|
int len;
|
|
try {
|
|
digest = MessageDigest.getInstance("MD5");
|
|
in = new FileInputStream(file);
|
|
while ((len = in.read(buffer, 0, 1024)) != -1) {
|
|
digest.update(buffer, 0, len);
|
|
}
|
|
in.close();
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
return "";
|
|
}
|
|
BigInteger bigInt = new BigInteger(1, digest.digest());
|
|
return bigInt.toString(32);
|
|
}
|
|
|
|
public static String getDownLoadPath(Context context) {
|
|
String path = ContextCompat.getExternalFilesDirs(context, Environment.DIRECTORY_DOWNLOADS)[0].getAbsolutePath();
|
|
return path + File.separator;
|
|
}
|
|
|
|
// public static void ariaDownload(Context context, String url, AppDetails appDetails) {
|
|
// Log.e(TAG, "ariaDownload: " + appDetails);
|
|
// AriaDownloadInfo ariaDownloadInfo = AriaDownloadInfo.toAriaDownloadInfo(appDetails);
|
|
// ariaDownload(context, url, ariaDownloadInfo);
|
|
// }
|
|
|
|
public static void ariaDownload(Context context, String url, AppUpdateInfo appUpdateInfo) {
|
|
Log.e(TAG, "ariaDownload: " + appUpdateInfo);
|
|
AriaDownloadInfo ariaDownloadInfo = AriaDownloadInfo.toAriaDownloadInfo(appUpdateInfo);
|
|
ariaDownload(context, url, ariaDownloadInfo);
|
|
}
|
|
|
|
public static void ariaDownload(Context context, String url, AriaDownloadInfo ariaDownloadInfo) {
|
|
Log.e(TAG, "ariaDownload: " + ariaDownloadInfo);
|
|
String fileName = getFileNamefromURL(url);
|
|
String app_md5 = ariaDownloadInfo.getAppMd5();
|
|
Log.e("ariaDownload", "app_md5 = " + app_md5);
|
|
File file = new File(getDownLoadPath(context) + fileName);
|
|
if (file.exists() && !file.isDirectory()) {
|
|
String fileMd5 = com.blankj.utilcode.util.FileUtils.getFileMD5ToString(file);
|
|
Log.e("ariaDownload", "fileMD5 = " + fileMd5);
|
|
if (fileMd5.equalsIgnoreCase(app_md5)) {
|
|
ApkUtils.installApkFile(context, file);
|
|
} else {
|
|
file.delete();
|
|
Aria.download(context)
|
|
.load(url) //读取下载地址
|
|
.setFilePath(getDownLoadPath(context) + fileName)
|
|
.ignoreFilePathOccupy()
|
|
.setExtendField(GsonUtils.toJSONString(ariaDownloadInfo))
|
|
.create(); //启动下载}
|
|
}
|
|
} else {
|
|
Aria.download(context)
|
|
.load(url) //读取下载地址
|
|
.setFilePath(getDownLoadPath(context) + fileName)
|
|
.ignoreFilePathOccupy()
|
|
.setExtendField(GsonUtils.toJSONString(ariaDownloadInfo))
|
|
.create(); //启动下载}
|
|
}
|
|
}
|
|
}
|