Search in sources :

Example 21 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)

Example 22 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 23 with ParcelFileDescriptor

use of android.os.ParcelFileDescriptor in project remusic by aa112901.

the class ImageUtils method getArtworkQuick.

// Get album art for specified album. This method will not try to
// fall back to getting artwork directly from the file, nor will
// it attempt to repair the database.
public static Bitmap getArtworkQuick(Context context, long album_id, int w, int h) {
    // NOTE: There is in fact a 1 pixel border on the right side in the
    // ImageView
    // used to display this drawable. Take it into account now, so we don't
    // have to
    // scale later.
    w -= 1;
    ContentResolver res = context.getContentResolver();
    Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
    if (uri != null) {
        ParcelFileDescriptor fd = null;
        try {
            fd = res.openFileDescriptor(uri, "r");
            if (fd == null) {
                return null;
            }
            int sampleSize = 1;
            // Compute the closest power-of-two scale factor
            // and pass that to sBitmapOptionsCache.inSampleSize, which will
            // result in faster decoding and better quality
            sBitmapOptionsCache.inJustDecodeBounds = true;
            BitmapFactory.decodeFileDescriptor(fd.getFileDescriptor(), null, sBitmapOptionsCache);
            int nextWidth = sBitmapOptionsCache.outWidth >> 1;
            int nextHeight = sBitmapOptionsCache.outHeight >> 1;
            while (nextWidth > w && nextHeight > h) {
                sampleSize <<= 1;
                nextWidth >>= 1;
                nextHeight >>= 1;
            }
            sBitmapOptionsCache.inSampleSize = sampleSize;
            sBitmapOptionsCache.inJustDecodeBounds = false;
            Bitmap b = BitmapFactory.decodeFileDescriptor(fd.getFileDescriptor(), null, sBitmapOptionsCache);
            if (b != null) {
                // finally rescale to exactly the size we need
                if (sBitmapOptionsCache.outWidth != w || sBitmapOptionsCache.outHeight != h) {
                    Bitmap tmp = Bitmap.createScaledBitmap(b, w, h, true);
                    // bitmap
                    if (tmp != b)
                        b.recycle();
                    b = tmp;
                }
            }
            return b;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fd != null)
                    fd.close();
            } catch (IOException e) {
            }
        }
    }
    return null;
}
Also used : Bitmap(android.graphics.Bitmap) ParcelFileDescriptor(android.os.ParcelFileDescriptor) IOException(java.io.IOException) Uri(android.net.Uri) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ContentResolver(android.content.ContentResolver)

Example 24 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 25 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)

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