Search in sources :

Example 71 with ParcelFileDescriptor

use of android.os.ParcelFileDescriptor in project qksms by moezbhatti.

the class TempFileProvider method getTempStoreFd.

private ParcelFileDescriptor getTempStoreFd(String mode) {
    String fileName = getScrapPath(getContext());
    ParcelFileDescriptor pfd = null;
    try {
        File file = new File(fileName);
        // make sure the path is valid and directories created for this file.
        File parentFile = file.getParentFile();
        if (!parentFile.exists() && !parentFile.mkdirs()) {
            Log.e(TAG, "[TempFileProvider] tempStoreFd: " + parentFile.getPath() + "does not exist!");
            return null;
        }
        int modeFlags;
        if (mode.equals("r")) {
            modeFlags = ParcelFileDescriptor.MODE_READ_ONLY;
        } else {
            modeFlags = ParcelFileDescriptor.MODE_READ_WRITE | ParcelFileDescriptor.MODE_CREATE | ParcelFileDescriptor.MODE_TRUNCATE;
        }
        pfd = ParcelFileDescriptor.open(file, modeFlags);
    } catch (Exception ex) {
        Log.e(TAG, "getTempStoreFd: error creating pfd for " + fileName, ex);
    }
    return pfd;
}
Also used : ParcelFileDescriptor(android.os.ParcelFileDescriptor) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException)

Example 72 with ParcelFileDescriptor

use of android.os.ParcelFileDescriptor in project k-9 by k9mail.

the class ParcelFileDescriptorUtil method pipeFrom.

public static ParcelFileDescriptor pipeFrom(InputStream inputStream) throws IOException {
    ParcelFileDescriptor[] pipe = ParcelFileDescriptor.createPipe();
    ParcelFileDescriptor readSide = pipe[0];
    ParcelFileDescriptor writeSide = pipe[1];
    new TransferThread(inputStream, new ParcelFileDescriptor.AutoCloseOutputStream(writeSide)).start();
    return readSide;
}
Also used : ParcelFileDescriptor(android.os.ParcelFileDescriptor)

Example 73 with ParcelFileDescriptor

use of android.os.ParcelFileDescriptor in project OpenMEAP by OpenMEAP.

the class FileContentProvider method openFile.

@Override
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
    if (!"r".equals(mode)) {
        throw new FileNotFoundException("The SLIC FileContentProvider does not support mode \"" + mode + "\" for " + uri);
    }
    String rootOmPath = System.getProperty("root.openmeap.path");
    String filename = uri.toString().substring(BASE_URI_LEN);
    String relFN = filename.replace(rootOmPath, "");
    filename = rootOmPath + FILE_SEP + getInternalStorageFileName(relFN);
    File file = new File(filename);
    ParcelFileDescriptor toRet = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
    return toRet;
}
Also used : FileNotFoundException(java.io.FileNotFoundException) ParcelFileDescriptor(android.os.ParcelFileDescriptor) File(java.io.File)

Example 74 with ParcelFileDescriptor

use of android.os.ParcelFileDescriptor in project SeriesGuide by UweTrottmann.

the class JsonExportTask method exportData.

private int exportData(File exportPath, @BackupType int type) {
    // check if there is any data to export
    Cursor data = getDataCursor(type);
    if (data == null) {
        // query failed
        return ERROR;
    }
    if (data.getCount() == 0) {
        // There is no data? Done.
        data.close();
        return SUCCESS;
    }
    publishProgress(data.getCount(), 0);
    // try to export all data
    try {
        if (!isUseDefaultFolders) {
            // ensure the user has selected a backup file
            Uri backupFileUri = getDataBackupFile(type);
            if (backupFileUri == null) {
                return ERROR_FILE_ACCESS;
            }
            ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(backupFileUri, "w");
            if (pfd == null) {
                return ERROR_FILE_ACCESS;
            }
            FileOutputStream out = new FileOutputStream(pfd.getFileDescriptor());
            if (type == BACKUP_SHOWS) {
                writeJsonStreamShows(out, data);
            } else if (type == BACKUP_LISTS) {
                writeJsonStreamLists(out, data);
            } else if (type == BACKUP_MOVIES) {
                writeJsonStreamMovies(out, data);
            }
            // let the document provider know we're done.
            pfd.close();
        } else {
            File backupFile;
            if (type == BACKUP_SHOWS) {
                backupFile = new File(exportPath, EXPORT_JSON_FILE_SHOWS);
            } else if (type == BACKUP_LISTS) {
                backupFile = new File(exportPath, EXPORT_JSON_FILE_LISTS);
            } else if (type == BACKUP_MOVIES) {
                backupFile = new File(exportPath, EXPORT_JSON_FILE_MOVIES);
            } else {
                return ERROR;
            }
            OutputStream out = new FileOutputStream(backupFile);
            if (type == BACKUP_SHOWS) {
                writeJsonStreamShows(out, data);
            } else if (type == BACKUP_LISTS) {
                writeJsonStreamLists(out, data);
            } else {
                writeJsonStreamMovies(out, data);
            }
        }
    } catch (FileNotFoundException e) {
        Timber.e(e, "Backup file not found.");
        removeBackupFileUri(type);
        return ERROR_FILE_ACCESS;
    } catch (IOException | SecurityException e) {
        Timber.e(e, "Could not access backup file.");
        removeBackupFileUri(type);
        return ERROR_FILE_ACCESS;
    } catch (JsonParseException e) {
        Timber.e(e, "JSON export failed.");
        return ERROR;
    } finally {
        data.close();
    }
    return SUCCESS;
}
Also used : FileOutputStream(java.io.FileOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ParcelFileDescriptor(android.os.ParcelFileDescriptor) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Cursor(android.database.Cursor) JsonParseException(com.google.gson.JsonParseException) Uri(android.net.Uri) File(java.io.File)

Example 75 with ParcelFileDescriptor

use of android.os.ParcelFileDescriptor in project SeriesGuide by UweTrottmann.

the class JsonImportTask method importData.

private int importData(File importPath, @JsonExportTask.BackupType int type) {
    // so make sure to not fail just because a default folder file is missing
    if (!isUseDefaultFolders) {
        // make sure we have a file uri...
        Uri backupFileUri = getDataBackupFile(type);
        if (backupFileUri == null) {
            return ERROR_FILE_ACCESS;
        }
        // ...and the file actually exists
        ParcelFileDescriptor pfd;
        try {
            pfd = context.getContentResolver().openFileDescriptor(backupFileUri, "r");
        } catch (FileNotFoundException | SecurityException e) {
            Timber.e(e, "Backup file not found.");
            return ERROR_FILE_ACCESS;
        }
        if (pfd == null) {
            Timber.e("File descriptor is null.");
            return ERROR_FILE_ACCESS;
        }
        clearExistingData(type);
        // Access JSON from backup file and try to import data
        FileInputStream in = new FileInputStream(pfd.getFileDescriptor());
        try {
            importFromJson(type, in);
            // let the document provider know we're done.
            pfd.close();
        } catch (JsonParseException | IOException | IllegalStateException e) {
            // the given Json might not be valid or unreadable
            Timber.e(e, "JSON import failed");
            return ERROR;
        }
    } else {
        // make sure we can access the backup file
        File backupFile = null;
        if (type == JsonExportTask.BACKUP_SHOWS) {
            backupFile = new File(importPath, JsonExportTask.EXPORT_JSON_FILE_SHOWS);
        } else if (type == JsonExportTask.BACKUP_LISTS) {
            backupFile = new File(importPath, JsonExportTask.EXPORT_JSON_FILE_LISTS);
        } else if (type == JsonExportTask.BACKUP_MOVIES) {
            backupFile = new File(importPath, JsonExportTask.EXPORT_JSON_FILE_MOVIES);
        }
        if (backupFile == null || !backupFile.canRead()) {
            return ERROR_FILE_ACCESS;
        }
        if (!backupFile.exists()) {
            // no backup file, so nothing to restore, skip it
            return SUCCESS;
        }
        FileInputStream in;
        try {
            in = new FileInputStream(backupFile);
        } catch (FileNotFoundException e) {
            Timber.e(e, "Backup file not found.");
            return ERROR_FILE_ACCESS;
        }
        clearExistingData(type);
        // Access JSON from backup file and try to import data
        try {
            importFromJson(type, in);
        } catch (JsonParseException | IOException | IllegalStateException e) {
            // the given Json might not be valid or unreadable
            Timber.e(e, "JSON show import failed");
            return ERROR;
        }
    }
    return SUCCESS;
}
Also used : ParcelFileDescriptor(android.os.ParcelFileDescriptor) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) JsonParseException(com.google.gson.JsonParseException) Uri(android.net.Uri) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

ParcelFileDescriptor (android.os.ParcelFileDescriptor)526 IOException (java.io.IOException)199 FileNotFoundException (java.io.FileNotFoundException)136 RemoteException (android.os.RemoteException)127 File (java.io.File)127 FileDescriptor (java.io.FileDescriptor)58 FileOutputStream (java.io.FileOutputStream)58 AssetFileDescriptor (android.content.res.AssetFileDescriptor)44 FileInputStream (java.io.FileInputStream)36 Parcel (android.os.Parcel)35 Uri (android.net.Uri)33 Intent (android.content.Intent)30 Bundle (android.os.Bundle)29 Cursor (android.database.Cursor)27 StorageManager (android.os.storage.StorageManager)25 Request (android.app.DownloadManager.Request)24 Bitmap (android.graphics.Bitmap)22 InputStream (java.io.InputStream)18 ProfilerInfo (android.app.ProfilerInfo)17 Binder (android.os.Binder)17