From 1f599c660f5e24546781bed10c2045018d170039 Mon Sep 17 00:00:00 2001 From: cpfeiffer Date: Wed, 28 Oct 2015 23:54:08 +0100 Subject: [PATCH] Improved file and error handling --- .../service/AbstractDeviceSupport.java | 7 +++++-- .../freeyourgadget/gadgetbridge/util/GB.java | 18 +++++------------- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/AbstractDeviceSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/AbstractDeviceSupport.java index cc499efd..ec2b8f20 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/AbstractDeviceSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/AbstractDeviceSupport.java @@ -15,6 +15,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; +import java.io.IOException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; @@ -171,8 +172,8 @@ public abstract class AbstractDeviceSupport implements DeviceSupport { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd-hhmmss"); String filename = "screenshot_" + dateFormat.format(new Date()) + ".bmp"; - if (GB.writeScreenshot(screenshot, filename)) { - String fullpath = context.getExternalFilesDir(null).toString() + "/" + filename; + try { + String fullpath = GB.writeScreenshot(screenshot, filename); Bitmap bmp = BitmapFactory.decodeFile(fullpath); Intent intent = new Intent(); intent.setAction(android.content.Intent.ACTION_VIEW); @@ -203,6 +204,8 @@ public abstract class AbstractDeviceSupport implements DeviceSupport { NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); nm.notify(NOTIFICATION_ID_SCREENSHOT, notif); + } catch (IOException ex) { + LOG.error("Error writing screenshot", ex); } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/GB.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/GB.java index ba798860..1c055127 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/GB.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/GB.java @@ -106,19 +106,15 @@ public class GB { return String.valueOf(rssi); } - public static boolean writeScreenshot(GBDeviceEventScreenshot screenshot, String filename) { + public static String writeScreenshot(GBDeviceEventScreenshot screenshot, String filename) throws IOException { LOG.info("Will write screenshot: " + screenshot.width + "x" + screenshot.height + "x" + screenshot.bpp + "bpp"); final int FILE_HEADER_SIZE = 14; final int INFO_HEADER_SIZE = 40; - File dir = GBApplication.getContext().getExternalFilesDir(null); - if (dir != null) { - if (!dir.exists()) { - dir.mkdirs(); - } - } - try (FileOutputStream fos = new FileOutputStream(dir + "/" + filename)) { + File dir = FileUtils.getExternalFilesDir(); + File outputFile = new File(dir, filename); + try (FileOutputStream fos = new FileOutputStream(outputFile)) { ByteBuffer headerbuf = ByteBuffer.allocate(FILE_HEADER_SIZE + INFO_HEADER_SIZE + screenshot.clut.length); headerbuf.order(ByteOrder.LITTLE_ENDIAN); @@ -149,12 +145,8 @@ public class GB { fos.write(screenshot.data, rowbytes * i, rowbytes); fos.write(pad); } - } catch (IOException e) { - LOG.error("Error saving screenshot", e); - return false; } - - return true; + return outputFile.getAbsolutePath(); } /**