Example usage for android.content.res AssetFileDescriptor UNKNOWN_LENGTH

List of usage examples for android.content.res AssetFileDescriptor UNKNOWN_LENGTH

Introduction

In this page you can find the example usage for android.content.res AssetFileDescriptor UNKNOWN_LENGTH.

Prototype

long UNKNOWN_LENGTH

To view the source code for android.content.res AssetFileDescriptor UNKNOWN_LENGTH.

Click Source Link

Document

Length used with #AssetFileDescriptor(ParcelFileDescriptor,long,long) and #getDeclaredLength when a length has not been declared.

Usage

From source file:com.raspi.chatapp.util.storage.file.LocalStorageProvider.java

@Override
public AssetFileDescriptor openDocumentThumbnail(final String documentId, final Point sizeHint,
        final CancellationSignal signal) throws FileNotFoundException {
    if (LocalStorageProvider.isMissingPermission(getContext())) {
        return null;
    }/*from  w  ww  . j  a v a2 s .  c  om*/
    // Assume documentId points to an image file. Build a thumbnail no larger than twice the sizeHint
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(documentId, options);
    final int targetHeight = 2 * sizeHint.y;
    final int targetWidth = 2 * sizeHint.x;
    final int height = options.outHeight;
    final int width = options.outWidth;
    options.inSampleSize = 1;
    if (height > targetHeight || width > targetWidth) {
        final int halfHeight = height / 2;
        final int halfWidth = width / 2;
        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / options.inSampleSize) > targetHeight
                || (halfWidth / options.inSampleSize) > targetWidth) {
            options.inSampleSize *= 2;
        }
    }
    options.inJustDecodeBounds = false;
    Bitmap bitmap = BitmapFactory.decodeFile(documentId, options);
    // Write out the thumbnail to a temporary file
    File tempFile = null;
    FileOutputStream out = null;
    try {
        tempFile = File.createTempFile("thumbnail", null, getContext().getCacheDir());
        out = new FileOutputStream(tempFile);
        bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
    } catch (IOException e) {
        Log.e(LocalStorageProvider.class.getSimpleName(), "Error writing thumbnail", e);
        return null;
    } finally {
        if (out != null)
            try {
                out.close();
            } catch (IOException e) {
                Log.e(LocalStorageProvider.class.getSimpleName(), "Error closing thumbnail", e);
            }
    }
    // It appears the Storage Framework UI caches these results quite aggressively so there is little reason to
    // write your own caching layer beyond what you need to return a single AssetFileDescriptor
    return new AssetFileDescriptor(ParcelFileDescriptor.open(tempFile, ParcelFileDescriptor.MODE_READ_ONLY), 0,
            AssetFileDescriptor.UNKNOWN_LENGTH);
}

From source file:com.seafile.seadroid2.provider.SeafileProvider.java

@Override
public AssetFileDescriptor openDocumentThumbnail(String documentId, Point sizeHint, CancellationSignal signal)
        throws FileNotFoundException {

    Log.d(DEBUG_TAG, "openDocumentThumbnail(): " + documentId);

    String repoId = DocumentIdParser.getRepoIdFromId(documentId);
    if (repoId.isEmpty()) {
        throw new FileNotFoundException();
    }//from   w  w  w.j  ava2s.co m

    String mimeType = Utils.getFileMimeType(documentId);
    if (!mimeType.startsWith("image/")) {
        throw new FileNotFoundException();
    }

    DataManager dm = createDataManager(documentId);

    String path = DocumentIdParser.getPathFromId(documentId);

    final DisplayImageOptions options = new DisplayImageOptions.Builder().extraForDownloader(dm.getAccount())
            .cacheInMemory(false) // SAF does its own caching
            .cacheOnDisk(true).considerExifParams(true).build();

    final ParcelFileDescriptor[] pair;
    try {
        pair = ParcelFileDescriptor.createReliablePipe();
    } catch (IOException e) {
        throw new FileNotFoundException();
    }

    final String url = dm.getThumbnailLink(repoId, path, sizeHint.x);
    if (url == null)
        throw new FileNotFoundException();

    // do thumbnail download in another thread to avoid possible network access in UI thread
    final Future future = ConcurrentAsyncTask.submit(new Runnable() {

        @Override
        public void run() {
            try {
                FileOutputStream fileStream = new FileOutputStream(pair[1].getFileDescriptor());

                // load the file. this might involve talking to the seafile server. this will hang until
                // it is done.
                Bitmap bmp = ImageLoader.getInstance().loadImageSync(url, options);

                if (bmp != null) {
                    bmp.compress(Bitmap.CompressFormat.PNG, 100, fileStream);
                }
            } finally {
                IOUtils.closeQuietly(pair[1]);
            }
        }
    });

    if (signal != null) {
        signal.setOnCancelListener(new CancellationSignal.OnCancelListener() {
            @Override
            public void onCancel() {
                Log.d(DEBUG_TAG, "openDocumentThumbnail() cancelling download");
                future.cancel(true);
                IOUtils.closeQuietly(pair[1]);
            }
        });
    }

    return new AssetFileDescriptor(pair[0], 0, AssetFileDescriptor.UNKNOWN_LENGTH);
}