public static FileInfo getContactAsVCardFile(Context context, Uri uri) { AssetFileDescriptor fd; try { fd = context.getContentResolver().openAssetFileDescriptor(uri, "r"); java.io.FileInputStream in = fd.createInputStream(); byte[] buf = new byte[(int) fd.getDeclaredLength()]; in.read(buf); in.close(); String vCardText = new String(buf); Log.d("Vcard", vCardText); List<String> pathSegments = uri.getPathSegments(); String targetPath = "/" + pathSegments.get(pathSegments.size() - 1) + ".vcf"; SecureMediaStore.copyToVfs(buf, targetPath); FileInfo info = new FileInfo(); info.path = SecureMediaStore.vfsUri(targetPath).toString(); info.type = "text/vcard"; return info; } catch (Exception e) { e.printStackTrace(); } return null; }
public static FileInfo getFileInfoFromURI(Context aContext, Uri uri) throws IllegalArgumentException { FileInfo info = new FileInfo(); info.path = uri.toString(); if (SecureMediaStore.isVfsUri(uri)) { info.path = uri.getPath(); info.type = getMimeType(uri.toString()); return info; } if (uri.getScheme() != null && uri.getScheme().equals("file")) { info.path = uri.getPath(); info.type = getMimeType(uri.toString()); return info; } if (uri.toString().startsWith("content://org.openintents.filemanager/")) { // Work around URI escaping brokenness info.path = uri.toString().replaceFirst("content://org.openintents.filemanager", ""); info.type = getMimeType(uri.toString()); return info; } Cursor cursor = aContext.getContentResolver().query(uri, null, null, null, null); if (cursor != null && cursor.getCount() > 0) { cursor.moveToFirst(); // need to check columns for different types int dataIdx = cursor.getColumnIndex(MediaStore.Images.Media.DATA); if (dataIdx != -1) { info.path = cursor.getString(dataIdx); info.type = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.MIME_TYPE)); } else { dataIdx = cursor.getColumnIndex(MediaStore.Video.Media.DATA); if (dataIdx != -1) { info.path = cursor.getString(dataIdx); info.type = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE)); } else { dataIdx = cursor.getColumnIndex(MediaStore.Audio.Media.DATA); if (dataIdx != -1) { info.path = cursor.getString(dataIdx); info.type = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.MIME_TYPE)); } else { dataIdx = cursor.getColumnIndex(MediaStore.MediaColumns.DATA); if (dataIdx != -1) { info.path = cursor.getString(dataIdx); info.type = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.MIME_TYPE)); } } } } } if (cursor != null) cursor.close(); if (info.type == null) info.type = getMimeType(info.path); return info; }