List of usage examples for android.content.res AssetFileDescriptor AssetFileDescriptor
public AssetFileDescriptor(ParcelFileDescriptor fd, long startOffset, long length)
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; }/*www .j av a 2 s. co m*/ // 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 av a2s . 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); }
From source file:org.alfresco.mobile.android.application.providers.storage.StorageAccessDocumentsProvider.java
@Override public AssetFileDescriptor openDocumentThumbnail(String documentId, Point sizeHint, CancellationSignal signal) throws FileNotFoundException { // Log.v(TAG, "openDocumentThumbnail"); try {/* w w w. ja v a2 s .c om*/ Node currentNode = null; EncodedQueryUri cUri = new EncodedQueryUri(documentId); if (cUri.type != PREFIX_DOC) { return null; } checkSession(cUri); currentNode = retrieveNode(cUri.id); // Let's retrieve the thumbnail // Store the document inside a temporary folder per account File downloadedFile = null; if (getContext() != null && currentNode != null && session != null) { File folder = AlfrescoStorageManager.getInstance(getContext()).getTempFolder(selectedAccount); if (folder != null) { downloadedFile = new File(folder, currentNode.getName()); } } else { return null; } // Is Document in cache ? if (downloadedFile.exists() && currentNode.getModifiedAt().getTimeInMillis() < downloadedFile.lastModified()) { // Document available locally ParcelFileDescriptor pfd = ParcelFileDescriptor.open(downloadedFile, ParcelFileDescriptor.MODE_READ_ONLY); return new AssetFileDescriptor(pfd, 0, downloadedFile.length()); } // Not in cache so let's download the content ! ContentStream contentStream = session.getServiceRegistry().getDocumentFolderService() .getRenditionStream(currentNode, DocumentFolderService.RENDITION_THUMBNAIL); // Check ContentStream if (contentStream == null || contentStream.getLength() == 0) { return null; } // Store the thumbnail locally copyFile(contentStream.getInputStream(), contentStream.getLength(), downloadedFile, signal); // Return the fileDescriptor if (downloadedFile.exists()) { ParcelFileDescriptor pfd = ParcelFileDescriptor.open(downloadedFile, ParcelFileDescriptor.MODE_READ_ONLY); return new AssetFileDescriptor(pfd, 0, downloadedFile.length()); } else { return null; } } catch (Exception e) { Log.w(TAG, Log.getStackTraceString(e)); return null; } }
From source file:net.sf.xfd.provider.PublicProvider.java
@Nullable @Override//from w w w.j av a2 s . c om public AssetFileDescriptor openAssetFile(@NonNull Uri uri, @NonNull String mode, @Nullable CancellationSignal signal) throws FileNotFoundException { ParcelFileDescriptor fd = openFile(uri, mode, signal); return fd != null ? new AssetFileDescriptor(fd, 0, -1) : null; }