Improved file and error handling

master
cpfeiffer 2015-10-28 23:54:08 +01:00
parent 694b3d897f
commit 1f599c660f
2 changed files with 10 additions and 15 deletions

View File

@ -15,6 +15,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
@ -171,8 +172,8 @@ public abstract class AbstractDeviceSupport implements DeviceSupport {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd-hhmmss"); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd-hhmmss");
String filename = "screenshot_" + dateFormat.format(new Date()) + ".bmp"; String filename = "screenshot_" + dateFormat.format(new Date()) + ".bmp";
if (GB.writeScreenshot(screenshot, filename)) { try {
String fullpath = context.getExternalFilesDir(null).toString() + "/" + filename; String fullpath = GB.writeScreenshot(screenshot, filename);
Bitmap bmp = BitmapFactory.decodeFile(fullpath); Bitmap bmp = BitmapFactory.decodeFile(fullpath);
Intent intent = new Intent(); Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW); 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); NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(NOTIFICATION_ID_SCREENSHOT, notif); nm.notify(NOTIFICATION_ID_SCREENSHOT, notif);
} catch (IOException ex) {
LOG.error("Error writing screenshot", ex);
} }
} }

View File

@ -106,19 +106,15 @@ public class GB {
return String.valueOf(rssi); 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"); LOG.info("Will write screenshot: " + screenshot.width + "x" + screenshot.height + "x" + screenshot.bpp + "bpp");
final int FILE_HEADER_SIZE = 14; final int FILE_HEADER_SIZE = 14;
final int INFO_HEADER_SIZE = 40; final int INFO_HEADER_SIZE = 40;
File dir = GBApplication.getContext().getExternalFilesDir(null); File dir = FileUtils.getExternalFilesDir();
if (dir != null) { File outputFile = new File(dir, filename);
if (!dir.exists()) { try (FileOutputStream fos = new FileOutputStream(outputFile)) {
dir.mkdirs();
}
}
try (FileOutputStream fos = new FileOutputStream(dir + "/" + filename)) {
ByteBuffer headerbuf = ByteBuffer.allocate(FILE_HEADER_SIZE + INFO_HEADER_SIZE + screenshot.clut.length); ByteBuffer headerbuf = ByteBuffer.allocate(FILE_HEADER_SIZE + INFO_HEADER_SIZE + screenshot.clut.length);
headerbuf.order(ByteOrder.LITTLE_ENDIAN); headerbuf.order(ByteOrder.LITTLE_ENDIAN);
@ -149,12 +145,8 @@ public class GB {
fos.write(screenshot.data, rowbytes * i, rowbytes); fos.write(screenshot.data, rowbytes * i, rowbytes);
fos.write(pad); fos.write(pad);
} }
} catch (IOException e) {
LOG.error("Error saving screenshot", e);
return false;
} }
return outputFile.getAbsolutePath();
return true;
} }
/** /**