Search in sources :

Example 1 with MemoryFile

use of android.os.MemoryFile in project Signal-Android by signalapp.

the class PartProvider method getParcelStreamForAttachment.

private ParcelFileDescriptor getParcelStreamForAttachment(AttachmentId attachmentId) throws IOException {
    long plaintextLength = Util.getStreamLength(DatabaseFactory.getAttachmentDatabase(getContext()).getAttachmentStream(attachmentId, 0));
    MemoryFile memoryFile = new MemoryFile(attachmentId.toString(), Util.toIntExact(plaintextLength));
    InputStream in = DatabaseFactory.getAttachmentDatabase(getContext()).getAttachmentStream(attachmentId, 0);
    OutputStream out = memoryFile.getOutputStream();
    Util.copy(in, out);
    Util.close(out);
    Util.close(in);
    return MemoryFileUtil.getParcelFileDescriptor(memoryFile);
}
Also used : MemoryFile(android.os.MemoryFile) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream)

Example 2 with MemoryFile

use of android.os.MemoryFile in project android-database-sqlcipher by sqlcipher.

the class SQLiteContentHelper method getBlobColumnAsAssetFile.

/**
 * Runs an SQLite query and returns an AssetFileDescriptor for the
 * blob in column 0 of the first row. If the first column does
 * not contain a blob, an unspecified exception is thrown.
 *
 * @param db Handle to a readable database.
 * @param sql SQL query, possibly with query arguments.
 * @param selectionArgs Query argument values, or {@code null} for no argument.
 * @return If no exception is thrown, a non-null AssetFileDescriptor is returned.
 * @throws FileNotFoundException If the query returns no results or the
 *         value of column 0 is NULL, or if there is an error creating the
 *         asset file descriptor.
 */
public static AssetFileDescriptor getBlobColumnAsAssetFile(SQLiteDatabase db, String sql, String[] selectionArgs) throws FileNotFoundException {
    android.os.ParcelFileDescriptor fd = null;
    try {
        MemoryFile file = simpleQueryForBlobMemoryFile(db, sql, selectionArgs);
        if (file == null) {
            throw new FileNotFoundException("No results.");
        }
        Class c = file.getClass();
        try {
            java.lang.reflect.Method m = c.getDeclaredMethod("getParcelFileDescriptor");
            m.setAccessible(true);
            fd = (android.os.ParcelFileDescriptor) m.invoke(file);
        } catch (Exception e) {
            android.util.Log.i("SQLiteContentHelper", "SQLiteCursor.java: " + e);
        }
        AssetFileDescriptor afd = new AssetFileDescriptor(fd, 0, file.length());
        return afd;
    } catch (IOException ex) {
        throw new FileNotFoundException(ex.toString());
    }
}
Also used : MemoryFile(android.os.MemoryFile) AssetFileDescriptor(android.content.res.AssetFileDescriptor) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 3 with MemoryFile

use of android.os.MemoryFile in project fresco by facebook.

the class WebpDecodingTest method getMemoryFile.

private MemoryFile getMemoryFile(String path) {
    try {
        byte[] data = ByteStreams.toByteArray(getTestImageInputStream(path));
        MemoryFile memoryFile = new MemoryFile(null, data.length);
        memoryFile.allowPurging(false);
        memoryFile.writeBytes(data, 0, 0, data.length);
        return memoryFile;
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
}
Also used : MemoryFile(android.os.MemoryFile) IOException(java.io.IOException)

Example 4 with MemoryFile

use of android.os.MemoryFile in project fresco by facebook.

the class WebpDecodingTest method test_webp_lossless_decoding_filedescriptor_bitmap.

@Test
public void test_webp_lossless_decoding_filedescriptor_bitmap() throws Throwable {
    final MemoryFile memoryFile = getMemoryFile("webp_ll.webp");
    final Bitmap bitmap = mWebpBitmapFactory.decodeFileDescriptor(getMemoryFileDescriptor(memoryFile), null, null);
    memoryFile.close();
    assertBitmap(bitmap, 400, 301);
}
Also used : MemoryFile(android.os.MemoryFile) Bitmap(android.graphics.Bitmap) Test(org.junit.Test)

Example 5 with MemoryFile

use of android.os.MemoryFile in project fresco by facebook.

the class WebpDecodingTest method test_webp_plain_decoding_filedescriptor_bitmap.

@Test
public void test_webp_plain_decoding_filedescriptor_bitmap() throws Throwable {
    final MemoryFile memoryFile = getMemoryFile("webp_plain.webp");
    final Bitmap bitmap = mWebpBitmapFactory.decodeFileDescriptor(getMemoryFileDescriptor(memoryFile), null, null);
    memoryFile.close();
    assertBitmap(bitmap, 320, 214);
}
Also used : MemoryFile(android.os.MemoryFile) Bitmap(android.graphics.Bitmap) Test(org.junit.Test)

Aggregations

MemoryFile (android.os.MemoryFile)12 Bitmap (android.graphics.Bitmap)5 Test (org.junit.Test)5 IOException (java.io.IOException)3 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 AssetFileDescriptor (android.content.res.AssetFileDescriptor)1 Cursor (android.database.Cursor)1 SensorDirectChannel (android.hardware.SensorDirectChannel)1 PooledByteBufferInputStream (com.facebook.common.memory.PooledByteBufferInputStream)1 LimitedInputStream (com.facebook.common.streams.LimitedInputStream)1 FileDescriptor (java.io.FileDescriptor)1 FileNotFoundException (java.io.FileNotFoundException)1 Config (org.robolectric.annotation.Config)1