-
Notifications
You must be signed in to change notification settings - Fork 1
/
Images.java
64 lines (53 loc) · 2.22 KB
/
Images.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.example.yourpackage;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
/**
* Created by OWL Chile on 04-08-2017.
*/
public class Images {
private static int WIDTH = 600;
private static int HEIGHT = 800;
public static File createImageFile(Context context, String name) throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = name + "_" + timeStamp + "_";
File storageDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
return image;
}
public static void scaleImage(File file){
try{
Bitmap originalBitmap = ScalingUtilities.decodeFile(file, WIDTH, HEIGHT, ScalingUtilities.ScalingLogic.FIT);
Bitmap scaledBitmap = ScalingUtilities.createScaledBitmap(originalBitmap, WIDTH, HEIGHT, ScalingUtilities.ScalingLogic.FIT);
FileOutputStream outStream = new FileOutputStream(file);
scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 98, outStream);
outStream.flush();
outStream.close();
}catch (Exception e){
e.printStackTrace();
}
}
public static Bitmap fileToBitmap(File file){
return ScalingUtilities.decodeFile(file, WIDTH, HEIGHT, ScalingUtilities.ScalingLogic.FIT);
}
public static void deleteNoRequiredImages(Context context, String[] paths){
File storageDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
for(File picture : storageDir.listFiles()){
if(!Arrays.asList(paths).contains(picture.getAbsolutePath()))
picture.delete();
}
}
}