private void includeFile(final MatrixCursor result, final File file) throws FileNotFoundException { final MatrixCursor.RowBuilder row = result.newRow(); // These columns are required row.add(Document.COLUMN_DOCUMENT_ID, file.getAbsolutePath()); row.add(Document.COLUMN_DISPLAY_NAME, file.getName()); String mimeType = getDocumentType(file.getAbsolutePath()); row.add(Document.COLUMN_MIME_TYPE, mimeType); int flags = file.canWrite() ? Document.FLAG_SUPPORTS_DELETE | Document.FLAG_SUPPORTS_WRITE : 0; // We only show thumbnails for image files - expect a call to // openDocumentThumbnail for each file that has // this flag set if (mimeType.startsWith("image/")) flags |= Document.FLAG_SUPPORTS_THUMBNAIL; row.add(Document.COLUMN_FLAGS, flags); // COLUMN_SIZE is required, but can be null row.add(Document.COLUMN_SIZE, file.length()); // These columns are optional row.add(Document.COLUMN_LAST_MODIFIED, file.lastModified()); // Document.COLUMN_ICON can be a resource id identifying a custom icon. // The system provides default icons // based on mime type // Document.COLUMN_SUMMARY is optional additional information about the // file }
@Override public String getDocumentType(final String documentId) throws FileNotFoundException { File file = new File(documentId); if (file.isDirectory()) return Document.MIME_TYPE_DIR; // From FileProvider.getType(Uri) final int lastDot = file.getName().lastIndexOf('.'); if (lastDot >= 0) { final String extension = file.getName().substring(lastDot + 1); final String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); if (mime != null) { return mime; } } return "application/octet-stream"; }
@Override public void createFolder(String new_name) { try { Uri new_uri = DocumentsContract.createDocument(ctx.getContentResolver(), uri, Document.MIME_TYPE_DIR, new_name); if (new_uri != null) { notifyRefr(new_name); return; } } catch (Exception e) { e.printStackTrace(); } notify(ctx.getString(R.string.fman_create_folder_error, new_name), CommanderIf.OPERATION_FAILED); }
private void recursiveDeleteFolder(DocumentMetadata metadata) throws IOException { // Fetch the latest children just in case our cache is out of date. metadata.loadChildren(mClient); for (DocumentMetadata child : metadata.getChildren().values()) { if (Document.MIME_TYPE_DIR.equals(child.getMimeType())) { recursiveDeleteFolder(child); } else { deleteFile(child); } } final Uri uri = metadata.getUri(); mClient.rmdir(uri.toString()); mCache.remove(uri); revokeDocumentPermission(toDocumentId(uri)); }
private void includeFile(final MatrixCursor result, final File file) throws FileNotFoundException { final MatrixCursor.RowBuilder row = result.newRow(); // These columns are required row.add(Document.COLUMN_DOCUMENT_ID, file.getAbsolutePath()); row.add(Document.COLUMN_DISPLAY_NAME, file.getName()); String mimeType = getDocumentType(file.getAbsolutePath()); row.add(Document.COLUMN_MIME_TYPE, mimeType); int flags = file.canWrite() ? Document.FLAG_SUPPORTS_DELETE | Document.FLAG_SUPPORTS_WRITE | Document.FLAG_SUPPORTS_RENAME | (mimeType.equals(Document.MIME_TYPE_DIR) ? Document.FLAG_DIR_SUPPORTS_CREATE : 0) : 0; // We only show thumbnails for image files - expect a call to openDocumentThumbnail for each file that has // this flag set if (mimeType.startsWith("image/")) flags |= Document.FLAG_SUPPORTS_THUMBNAIL; row.add(Document.COLUMN_FLAGS, flags); // COLUMN_SIZE is required, but can be null row.add(Document.COLUMN_SIZE, file.length()); // These columns are optional row.add(Document.COLUMN_LAST_MODIFIED, file.lastModified()); // Document.COLUMN_ICON can be a resource id identifying a custom icon. The system provides default icons // based on mime type // Document.COLUMN_SUMMARY is optional additional information about the file }
@Override public String getDocumentType(final String documentId) throws FileNotFoundException { if (LocalStorageProvider.isMissingPermission(getContext())) { return null; } File file = new File(documentId); if (file.isDirectory()) return Document.MIME_TYPE_DIR; // From FileProvider.getType(Uri) final int lastDot = file.getName().lastIndexOf('.'); if (lastDot >= 0) { final String extension = file.getName().substring(lastDot + 1); final String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); if (mime != null) { return mime; } } return "application/octet-stream"; }
/** * Get display name from the uri * * @param resolver * @param uri * @return */ @TargetApi(Build.VERSION_CODES.KITKAT) private String getDisplayName(ContentResolver resolver, Uri uri) { String name = null; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { Cursor nameCursor = resolver.query(uri, null, null, null, null); try { if (nameCursor.getCount() > 0) { int displayNameIndex = nameCursor .getColumnIndex(Document.COLUMN_DISPLAY_NAME); if (displayNameIndex >= 0 && nameCursor.moveToFirst()) { name = nameCursor.getString(displayNameIndex); } } } finally { nameCursor.close(); } } return name; }