241 lines
8.3 KiB
Java
241 lines
8.3 KiB
Java
package com.jc.jcfw.util;
|
|
|
|
import android.content.ContentResolver;
|
|
import android.content.ContentUris;
|
|
import android.content.ContentValues;
|
|
import android.content.Context;
|
|
import android.database.Cursor;
|
|
import android.graphics.Bitmap;
|
|
import android.graphics.BitmapFactory;
|
|
import android.net.Uri;
|
|
import android.os.Build;
|
|
import android.os.Environment;
|
|
import android.provider.MediaStore;
|
|
import android.util.Log;
|
|
|
|
import org.json.JSONException;
|
|
import org.json.JSONObject;
|
|
|
|
import java.io.File;
|
|
import java.io.FileNotFoundException;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
import java.io.OutputStream;
|
|
import java.io.RandomAccessFile;
|
|
|
|
public class FileUtils {
|
|
private static final String TAG = FileUtils.class.getSimpleName();
|
|
/**
|
|
* check if path specificed exists, create it if not exists
|
|
*
|
|
* @param fileName path
|
|
* @return TRUE or FALSE
|
|
*/
|
|
public static boolean fileIsExist(String fileName) {
|
|
File file = new File(fileName);
|
|
if (file.exists())
|
|
return true;
|
|
else {
|
|
return file.mkdirs();
|
|
}
|
|
}
|
|
|
|
public static void writeFile(File filePath, String content) throws IOException {
|
|
FileOutputStream out = new FileOutputStream(filePath);
|
|
out.write(content.getBytes());
|
|
out.close();
|
|
}
|
|
|
|
public static JSONObject readJsonFromFile(File filePath) throws IOException, JSONException {
|
|
RandomAccessFile f = new RandomAccessFile(filePath, "r");
|
|
byte[] bytes = new byte[(int) f.length()];
|
|
f.readFully(bytes);
|
|
f.close();
|
|
return new JSONObject(new String(bytes));
|
|
}
|
|
|
|
/**
|
|
* get image base path of external storage
|
|
*/
|
|
public static String getPath(Context context) {
|
|
String fileName = "";
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
|
fileName = context.getExternalFilesDir("").getAbsolutePath() + "/current/";
|
|
} else {
|
|
if ("Xiaomi".equalsIgnoreCase(Build.BRAND)) {
|
|
fileName = Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera/";
|
|
} else if ("HUAWEI".equalsIgnoreCase(Build.BRAND)) {
|
|
fileName = Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera/";
|
|
} else if ("HONOR".equalsIgnoreCase(Build.BRAND)) {
|
|
fileName = Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera/";
|
|
} else if ("OPPO".equalsIgnoreCase(Build.BRAND)) {
|
|
fileName = Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera/";
|
|
} else if ("vivo".equalsIgnoreCase(Build.BRAND)) {
|
|
fileName = Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera/";
|
|
} else if ("samsung".equalsIgnoreCase(Build.BRAND)) {
|
|
fileName = Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera/";
|
|
} else {
|
|
fileName = Environment.getExternalStorageDirectory().getPath() + "/DCIM/";
|
|
}
|
|
}
|
|
File file = new File(fileName);
|
|
if (file.mkdirs()) {
|
|
return fileName;
|
|
}
|
|
return fileName;
|
|
}
|
|
|
|
public static String insertImageIntoGallery(Context activity, Bitmap source, String filename, String title) {
|
|
ContentResolver cr = activity.getContentResolver();
|
|
ContentValues values = new ContentValues();
|
|
values.put(MediaStore.Images.Media.TITLE, title);
|
|
values.put(MediaStore.Images.Media.DISPLAY_NAME, title);
|
|
values.put(MediaStore.Images.Media.DESCRIPTION, title);
|
|
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
|
|
// Add the date meta data to ensure the image is added at the front of the gallery
|
|
values.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis());
|
|
values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
|
values.put(MediaStore.Video.Media.IS_PENDING, 1);
|
|
}
|
|
|
|
Uri url = null;
|
|
String stringUrl = null; /* value to be returned */
|
|
|
|
try {
|
|
url = cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
|
|
|
|
if (source != null) {
|
|
OutputStream imageOut = cr.openOutputStream(url);
|
|
try {
|
|
source.compress(Bitmap.CompressFormat.JPEG, 100, imageOut);
|
|
} finally {
|
|
imageOut.close();
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
|
values.put(MediaStore.Video.Media.IS_PENDING, 0);
|
|
cr.update(url, values, null, null);
|
|
}
|
|
}
|
|
} else {
|
|
cr.delete(url, null, null);
|
|
return storeToAlternateSd(activity, source, filename);
|
|
// url = null;
|
|
}
|
|
} catch (Exception e) {
|
|
if (url != null) {
|
|
cr.delete(url, null, null);
|
|
// url = null;
|
|
}
|
|
return storeToAlternateSd(activity, source, filename);
|
|
}
|
|
if (url != null) {
|
|
stringUrl = url.toString();
|
|
}
|
|
|
|
return stringUrl;
|
|
}
|
|
|
|
/**
|
|
* If we have issues saving into our MediaStore, save it directly to our SD card. We can then interact with this file
|
|
* directly, opposed to pulling from the MediaStore. Again, this is a backup method if things don't work out as we
|
|
* would expect (seeing as most devices will have a MediaStore).
|
|
*
|
|
* @param src
|
|
* @return - the file's path
|
|
*/
|
|
private static String storeToAlternateSd(Context activity, Bitmap src, String filename){
|
|
if(src == null)
|
|
return null;
|
|
|
|
String sdCardDirectory = getPath(activity);
|
|
File image = new File(sdCardDirectory, filename + ".jpg");
|
|
try {
|
|
FileOutputStream imageOut = new FileOutputStream(image);
|
|
src.compress(Bitmap.CompressFormat.JPEG, 100, imageOut);
|
|
imageOut.close();
|
|
return image.getAbsolutePath();
|
|
} catch (IOException ex) {
|
|
ex.printStackTrace();
|
|
return null;
|
|
}
|
|
}
|
|
/*
|
|
* save bitmap to system gallery
|
|
*/
|
|
public static String saveBitmap(Context activity, String oid, Bitmap bitmap) {
|
|
String title = "wallet_key_" + oid;
|
|
String imageName = "wallet_key_" + oid;
|
|
String uri = insertImageIntoGallery(activity, bitmap, imageName, title);
|
|
Log.i(TAG, "save image success: " + uri);
|
|
return uri;
|
|
}
|
|
|
|
|
|
public static Bitmap loadImgData(Context activity, String oid) {
|
|
Uri uri = readImageFromGallery(activity, oid);
|
|
Bitmap data;
|
|
if (uri != null) {
|
|
try {
|
|
data = MediaStore.Images.Media.getBitmap(activity.getContentResolver(),uri);
|
|
} catch (IOException e) {
|
|
data = readImageFromExt(activity, oid);
|
|
}
|
|
} else {
|
|
data = readImageFromExt(activity, oid);
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
public static Bitmap readImageFromExt(Context activity, String oid) {
|
|
String sdCardDirectory = getPath(activity);
|
|
String imageName = "wallet_key_" + oid;
|
|
File file = new File(sdCardDirectory, imageName + ".jpg");
|
|
if (!file.exists()) {
|
|
return null;
|
|
}
|
|
Bitmap bitmap = null;
|
|
try {
|
|
bitmap = BitmapFactory.decodeFile(file.getPath());
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
return bitmap;
|
|
}
|
|
|
|
public static Uri readImageFromGallery(Context activity, String oid) {
|
|
String filename = "wallet_key_" + oid;
|
|
Uri uri;
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
|
uri = MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL);
|
|
} else {
|
|
uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
|
|
}
|
|
Uri result = null;
|
|
String[] projection = {MediaStore.Images.Media._ID,
|
|
MediaStore.Images.Media.DATA,
|
|
MediaStore.Images.Media.DISPLAY_NAME};
|
|
final String orderBy = MediaStore.Images.Media.DATE_ADDED;
|
|
Cursor cursor = activity.getContentResolver().query(uri, projection, null, null, orderBy + " DESC");
|
|
String imageName = "wallet_key_" + oid;
|
|
if (cursor != null) {
|
|
int nameColumn = cursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME);
|
|
int idColumn = cursor.getColumnIndex(MediaStore.Images.Media._ID);
|
|
int dataColumn = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
|
|
while (cursor.moveToNext()) {
|
|
String name = cursor.getString(nameColumn);
|
|
long id = cursor.getLong(idColumn);
|
|
Log.i(TAG, "img name: " + name + " id: " + id);
|
|
if (name.contains(imageName)) {
|
|
String data = cursor.getString(dataColumn);
|
|
result = ContentUris.withAppendedId(MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL), id);
|
|
Log.i(TAG, "img name: " + name + " id: " + id + " data:" + data);
|
|
break;
|
|
}
|
|
}
|
|
cursor.close();
|
|
}
|
|
return result;
|
|
}
|
|
}
|