Update FileUtil.java

This commit is contained in:
xC3FFF0E 2022-01-17 21:48:20 +08:00 committed by GitHub
parent ac8a30eb74
commit 80e030bc2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -47,8 +47,7 @@ makeDir(dirPath);
File file = new File(path);
try {
if (!file.exists())
file.createNewFile();
if (!file.exists()) file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
@ -140,6 +139,22 @@ e.printStackTrace();
}
}
public static void copyDir(String oldPath, String newPath) {
File oldFile = new File(oldPath);
File[] files = oldFile.listFiles();
File newFile = new File(newPath);
if (!newFile.exists()) {
newFile.mkdirs();
}
for (File file : files) {
if (file.isFile()) {
copyFile(file.getPath(), newPath + "/" + file.getName());
} else if (file.isDirectory()) {
copyDir(file.getPath(), newPath + "/" + file.getName());
}
}
}
public static void moveFile(String sourcePath, String destPath) {
copyFile(sourcePath, destPath);
deleteFile(sourcePath);
@ -263,7 +278,7 @@ contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = MediaStore.Audio.Media._ID + "=?";
final String selection = "_id=?";
final String[] selectionArgs = new String[]{
split[1]
};
@ -287,25 +302,18 @@ return null;
}
private static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
Cursor cursor = null;
final String column = MediaStore.Images.Media.DATA;
final String[] projection = {
column
};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
try (Cursor cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null)) {
if (cursor != null && cursor.moveToFirst()) {
final int column_index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(column_index);
}
} catch (Exception e) {
} finally {
if (cursor != null) {
cursor.close();
}
}
return null;
}
@ -324,21 +332,11 @@ return "com.android.providers.media.documents".equals(uri.getAuthority());
}
private static void saveBitmap(Bitmap bitmap, String destPath) {
FileOutputStream out = null;
FileUtil.createNewFile(destPath);
try {
out = new FileOutputStream(new File(destPath));
try (FileOutputStream out = new FileOutputStream(new File(destPath))) {
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
@ -456,26 +454,21 @@ Bitmap src = BitmapFactory.decodeFile(fromPath);
int width = src.getWidth();
int height = src.getHeight();
if (width < w && height < h)
return;
if (width < w && height < h) return;
int x = 0;
int y = 0;
if (width > w)
x = (width - w) / 2;
if (width > w) x = (width - w) / 2;
if (height > h)
y = (height - h) / 2;
if (height > h) y = (height - h) / 2;
int cw = w;
int ch = h;
if (w > width)
cw = width;
if (w > width) cw = width;
if (h > height)
ch = height;
if (h > height) ch = height;
Bitmap bitmap = Bitmap.createBitmap(src, x, y, cw, ch);
saveBitmap(bitmap, destPath);
@ -578,27 +571,25 @@ switch (iOrientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
default:
rotate = 0;
break;
}
}
catch (IOException e) {
} catch (IOException e) {
return 0;
}
return rotate;
}
public static File createNewPictureFile(Context context) {
SimpleDateFormat date = new SimpleDateFormat("yyyyMMdd_HHmmss");
String fileName = date.format(new Date()) + ".jpg";
File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_DCIM).getAbsolutePath() + File.separator + fileName);
return file;
return new File(context.getExternalFilesDir(Environment.DIRECTORY_DCIM).getAbsolutePath() + File.separator + fileName);
}
}