Improved file and error handling

here
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 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);
}
}

View File

@ -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();
}
/**