src/main/java/de/blau/android/prefs/PresetLoader.java
/**
     * Load a Preset from a local file
     * 
     * @param context an Android Context
     * @param uri the uri to load from
     * @param presetDir the directory to save the preset to
     * @param filename A filename where to save the file.
     * 
     * @return code indicating result
     */
Load a Preset from a local file

@param context an Android Context
@param uri the uri to load from
@param presetDir the directory to save the preset to
@param filename A filename where to save the file.

@return code indicating result
Load a Preset from a local file @param context an Android Context @param uri the uri to load from @param presetDir the directory to save the preset to @param filename A filename where to save the file . @return code indicating result
public static int load(@NonNull Context context, @NonNull Uri uri, @NonNull File presetDir, @NonNull String filename) {
        final ContentResolver contentResolver = context.getContentResolver();
        boolean zip = uri.getPath().toLowerCase(Locale.US).endsWith("." + FileExtensions.ZIP) || MimeTypes.ZIP.equals(contentResolver.getType(uri));
        if (zip) {
            Log.d(DEBUG_TAG, "detected zip file");
            filename = System.currentTimeMillis() + FILE_NAME_TEMPORARY_ARCHIVE;
        }
        final File destinationFile = new File(presetDir, filename);
        try (InputStream loadStream = contentResolver.openInputStream(uri); OutputStream fileStream = new FileOutputStream(destinationFile);) {
            Log.d(DEBUG_TAG, "Loading " + uri + " to " + presetDir + Paths.DELIMITER + filename);
            StreamUtils.copy(loadStream, fileStream);
            if (zip && FileUtil.unpackZip(presetDir.getPath() + Paths.DELIMITER, filename)) {
                if (!destinationFile.delete()) { 
                    Log.e(DEBUG_TAG, "Could not delete " + filename);
                }
                return DOWNLOADED_PRESET_ZIP;
            }
            return DOWNLOADED_PRESET_XML;
        } catch (Exception e) {
            Log.e(DEBUG_TAG, "Could not load file " + uri + " " + e.getMessage());
            return DOWNLOADED_PRESET_ERROR;
        }
    }
public static int load ( @ NonNull Context context , @ NonNull Uri uri , @ NonNull File presetDir , @ NonNull String filename ) { final ContentResolver contentResolver = context . getContentResolver ( ) ; boolean zip = uri . getPath ( ) . toLowerCase ( Locale . US ) . endsWith ( "." + FileExtensions . ZIP ) || MimeTypes . ZIP . equals ( contentResolver . getType ( uri ) ) ; if ( zip ) { Log . d ( DEBUG_TAG , "detected zip file" ) ; filename = System . currentTimeMillis ( ) + FILE_NAME_TEMPORARY_ARCHIVE ; } final File destinationFile = new File ( presetDir , filename ) ; try ( InputStream loadStream = contentResolver . openInputStream ( uri ) ; OutputStream fileStream = new FileOutputStream ( destinationFile ) ; ) { Log . d ( DEBUG_TAG , "Loading " + uri + " to " + presetDir + Paths . DELIMITER + filename ) ; StreamUtils . copy ( loadStream , fileStream ) ; if ( zip && FileUtil . unpackZip ( presetDir . getPath ( ) + Paths . DELIMITER , filename ) ) { if ( ! destinationFile . delete ( ) ) { Log . e ( DEBUG_TAG , "Could not delete " + filename ) ; } return DOWNLOADED_PRESET_ZIP ; } return DOWNLOADED_PRESET_XML ; } catch ( Exception e ) { Log . e ( DEBUG_TAG , "Could not load file " + uri + " " + e . getMessage ( ) ) ; return DOWNLOADED_PRESET_ERROR ; } }
Load a Preset from a local file
Load a Preset from a local file
// NOSONAR requires API 26