use of org.odk.collect.android.exception.GDriveConnectionException in project collect by opendatakit.
the class MediaUtils method getGoogleDriveFile.
private static File getGoogleDriveFile(Context context, Uri uri) throws GDriveConnectionException {
if (!Collect.getInstance().isNetworkAvailable()) {
throw new GDriveConnectionException();
}
if (uri == null) {
return null;
}
FileInputStream inputStream = null;
FileOutputStream outputStream = null;
String filePath = new File(context.getCacheDir(), "tmp").getAbsolutePath();
try {
ParcelFileDescriptor parcelFileDescriptor = context.getContentResolver().openFileDescriptor(uri, "r");
if (parcelFileDescriptor == null) {
return null;
}
FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
inputStream = new FileInputStream(fileDescriptor);
outputStream = new FileOutputStream(filePath);
int read;
byte[] bytes = new byte[4096];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
return new File(filePath);
} catch (IOException e) {
Timber.e(e);
} finally {
IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(outputStream);
}
return null;
}
use of org.odk.collect.android.exception.GDriveConnectionException in project collect by opendatakit.
the class FormEntryActivity method saveChosenImage.
private void saveChosenImage(Uri selectedImage) {
// Copy file to sdcard
String instanceFolder1 = getFormController().getInstanceFile().getParent();
String destImagePath = instanceFolder1 + File.separator + System.currentTimeMillis() + ".jpg";
File chosenImage;
try {
chosenImage = MediaUtils.getFileFromUri(this, selectedImage, Images.Media.DATA);
if (chosenImage != null) {
final File newImage = new File(destImagePath);
FileUtils.copyFile(chosenImage, newImage);
ImageConverter.execute(newImage.getPath(), getWidgetWaitingForBinaryData(), this);
runOnUiThread(() -> {
dismissDialog(SAVING_IMAGE_DIALOG);
if (getCurrentViewIfODKView() != null) {
getCurrentViewIfODKView().setBinaryData(newImage);
}
saveAnswersForCurrentScreen(DO_NOT_EVALUATE_CONSTRAINTS);
refreshCurrentView();
});
} else {
runOnUiThread(() -> {
dismissDialog(SAVING_IMAGE_DIALOG);
Timber.e("Could not receive chosen image");
showCustomToast(getString(R.string.error_occured), Toast.LENGTH_SHORT);
});
}
} catch (GDriveConnectionException e) {
runOnUiThread(() -> {
dismissDialog(SAVING_IMAGE_DIALOG);
Timber.e("Could not receive chosen image due to connection problem");
showCustomToast(getString(R.string.gdrive_connection_exception), Toast.LENGTH_LONG);
});
}
}
Aggregations