Search in sources :

Example 1 with GDriveConnectionException

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;
}
Also used : GDriveConnectionException(org.odk.collect.android.exception.GDriveConnectionException) FileOutputStream(java.io.FileOutputStream) ParcelFileDescriptor(android.os.ParcelFileDescriptor) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) ParcelFileDescriptor(android.os.ParcelFileDescriptor) FileDescriptor(java.io.FileDescriptor) SuppressLint(android.annotation.SuppressLint)

Example 2 with GDriveConnectionException

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);
        });
    }
}
Also used : GDriveConnectionException(org.odk.collect.android.exception.GDriveConnectionException) File(java.io.File)

Aggregations

File (java.io.File)2 GDriveConnectionException (org.odk.collect.android.exception.GDriveConnectionException)2 SuppressLint (android.annotation.SuppressLint)1 ParcelFileDescriptor (android.os.ParcelFileDescriptor)1 FileDescriptor (java.io.FileDescriptor)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1