You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
git-test/src/main/java/net/micode/notes/tool/ImageStorageManager.java

111 lines
3.3 KiB

package net.micode.notes.tool;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class ImageStorageManager {
private static final String TAG = "ImageStorageManager";
private static final String IMAGE_DIR = "note_images";
private static final int MAX_IMAGE_WIDTH = 800;
private static final int MAX_IMAGE_HEIGHT = 800;
private Context mContext;
public ImageStorageManager(Context context) {
mContext = context;
}
private File getImageDir() {
File dir = new File(mContext.getFilesDir(), IMAGE_DIR);
if (!dir.exists()) {
dir.mkdirs();
}
return dir;
}
public String saveImage(Bitmap bitmap, long noteId) {
try {
File imageDir = getImageDir();
String fileName = "note_" + noteId + "_" + System.currentTimeMillis() + ".jpg";
File imageFile = new File(imageDir, fileName);
FileOutputStream fos = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);
fos.flush();
fos.close();
Log.d(TAG, "Image saved to: " + imageFile.getAbsolutePath());
return imageFile.getAbsolutePath();
} catch (IOException e) {
Log.e(TAG, "Failed to save image", e);
return null;
}
}
public Bitmap loadImage(String imagePath) {
if (imagePath == null || imagePath.isEmpty()) {
return null;
}
try {
File imageFile = new File(imagePath);
if (!imageFile.exists()) {
Log.w(TAG, "Image file not found: " + imagePath);
return null;
}
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.RGB_565;
Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath(), options);
if (bitmap != null) {
Log.d(TAG, "Image loaded from: " + imagePath);
}
return bitmap;
} catch (Exception e) {
Log.e(TAG, "Failed to load image: " + imagePath, e);
return null;
}
}
public boolean deleteImage(String imagePath) {
if (imagePath == null || imagePath.isEmpty()) {
return false;
}
try {
File imageFile = new File(imagePath);
if (imageFile.exists()) {
boolean deleted = imageFile.delete();
if (deleted) {
Log.d(TAG, "Image deleted: " + imagePath);
}
return deleted;
}
return false;
} catch (Exception e) {
Log.e(TAG, "Failed to delete image: " + imagePath, e);
return false;
}
}
public void deleteNoteImages(long noteId) {
File imageDir = getImageDir();
File[] files = imageDir.listFiles();
if (files != null) {
for (File file : files) {
if (file.getName().startsWith("note_" + noteId + "_")) {
file.delete();
Log.d(TAG, "Deleted image: " + file.getName());
}
}
}
}
}