List of usage examples for android.support.v4.provider DocumentFile fromSingleUri
public static DocumentFile fromSingleUri(Context context, Uri uri)
From source file:com.maskyn.fileeditorpro.dialogfragment.FileInfoDialog.java
@Override public Dialog onCreateDialog(Bundle savedInstanceState) { View view = new DialogHelper.Builder(getActivity()).setTitle(R.string.info) .setView(R.layout.dialog_fragment_file_info).createSkeletonView(); //final View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_fragment_file_info, null); ListView list = (ListView) view.findViewById(android.R.id.list); DocumentFile file = DocumentFile.fromFile( new File(AccessStorageApi.getPath(getActivity(), (Uri) getArguments().getParcelable("uri")))); if (file == null && Device.hasKitKatApi()) { file = DocumentFile.fromSingleUri(getActivity(), (Uri) getArguments().getParcelable("uri")); }/*from ww w .ja v a2 s . c o m*/ // Get the last modification information. Long lastModified = file.lastModified(); // Create a new date object and pass last modified information // to the date object. Date date = new Date(lastModified); String[] lines1 = { getString(R.string.name), //getString(R.string.folder), getString(R.string.size), getString(R.string.modification_date) }; String[] lines2 = { file.getName(), //file.getParent(), FileUtils.byteCountToDisplaySize(file.length()), date.toString() }; list.setAdapter(new AdapterTwoItem(getActivity(), lines1, lines2)); return new AlertDialog.Builder(getActivity()).setView(view) .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }).create(); }
From source file:com.commonsware.android.tte.DocumentStorageService.java
private void load(Uri document) { try {//from www . j av a2s . c o m boolean weHavePermission = false; boolean isContent = ContentResolver.SCHEME_CONTENT.equals(document.getScheme()); if (isContent) { int perms = Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION; getContentResolver().takePersistableUriPermission(document, perms); for (UriPermission perm : getContentResolver().getPersistedUriPermissions()) { if (perm.getUri().equals(document)) { weHavePermission = true; } } } else { weHavePermission = true; } if (weHavePermission) { try { InputStream is = getContentResolver().openInputStream(document); try { String text = slurp(is); DocumentFile docFile; if (isContent) { docFile = DocumentFile.fromSingleUri(this, document); } else { docFile = DocumentFile.fromFile(new File(document.getPath())); } EventBus.getDefault().post( new DocumentLoadedEvent(document, text, docFile.getName(), docFile.canWrite())); } finally { is.close(); } } catch (Exception e) { Log.e(getClass().getSimpleName(), "Exception loading " + document.toString(), e); EventBus.getDefault().post(new DocumentLoadErrorEvent(document, e)); } } else { Log.e(getClass().getSimpleName(), "We failed to get permissions for " + document.toString()); EventBus.getDefault().post(new DocumentPermissionFailureEvent(document)); } } catch (SecurityException e) { Log.e(getClass().getSimpleName(), "Exception getting permissions for " + document.toString(), e); EventBus.getDefault().post(new DocumentPermissionFailureEvent(document)); } }
From source file:com.xxxifan.devbox.library.rxfile.RxFile.java
private static File fileFromUri(Context context, Uri data) throws Exception { DocumentFile file = DocumentFile.fromSingleUri(context, data); String fileType = file.getType(); String fileName = file.getName(); File fileCreated;/*from w w w . j a v a2 s. co m*/ ParcelFileDescriptor parcelFileDescriptor = context.getContentResolver().openFileDescriptor(data, Constants.READ_MODE); InputStream inputStream = new FileInputStream(parcelFileDescriptor.getFileDescriptor()); Log.e(TAG, "External cache dir:" + context.getExternalCacheDir()); String filePath = context.getExternalCacheDir() + Constants.FOLDER_SEPARATOR + fileName; String fileExtension = fileName.substring((fileName.lastIndexOf('.')) + 1); String mimeType = getMimeType(fileName); Log.e(TAG, "From Drive guessed type: " + getMimeType(fileName)); Log.e(TAG, "Extension: " + fileExtension); if (fileType.equals(Constants.APPLICATION_PDF) && mimeType == null) { filePath += "." + Constants.PDF_EXTENSION; } if (!createFile(filePath)) { return new File(filePath); } ReadableByteChannel from = Channels.newChannel(inputStream); WritableByteChannel to = Channels.newChannel(new FileOutputStream(filePath)); fastChannelCopy(from, to); from.close(); to.close(); fileCreated = new File(filePath); Log.e(TAG, "Path for made file: " + fileCreated.getAbsolutePath()); return fileCreated; }
From source file:com.commonsware.android.documents.consumer.DurablizerService.java
private DocumentFile buildDocFileForUri(Uri document) { DocumentFile docFile;// w ww. j av a 2 s .co m if (document.getScheme().equals(ContentResolver.SCHEME_CONTENT)) { docFile = DocumentFile.fromSingleUri(this, document); } else { docFile = DocumentFile.fromFile(new File(document.getPath())); } return (docFile); }
From source file:com.pavlospt.rxfile.RxFile.java
private static Observable<Bitmap> getThumbnailFromUriWithSizeAndKind(final Context context, final Uri data, final int requiredWidth, final int requiredHeight, final int kind) { return Observable.fromCallable(new Func0<Bitmap>() { @Override/*from ww w . j a v a 2 s. com*/ public Bitmap call() { Bitmap bitmap = null; ParcelFileDescriptor parcelFileDescriptor; final BitmapFactory.Options options = new BitmapFactory.Options(); if (requiredWidth > 0 && requiredHeight > 0) { options.inJustDecodeBounds = true; options.inSampleSize = calculateInSampleSize(options, requiredWidth, requiredHeight); options.inJustDecodeBounds = false; } if (!isMediaUri(data)) { logDebug("Not a media uri:" + data); if (isGoogleDriveDocument(data)) { logDebug("Google Drive Uri:" + data); DocumentFile file = DocumentFile.fromSingleUri(context, data); if (file.getType().startsWith(Constants.IMAGE_TYPE) || file.getType().startsWith(Constants.VIDEO_TYPE)) { logDebug("Google Drive Uri:" + data + " (Video or Image)"); try { parcelFileDescriptor = context.getContentResolver().openFileDescriptor(data, Constants.READ_MODE); FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor(); bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options); parcelFileDescriptor.close(); return bitmap; } catch (IOException e) { logError("Exception:" + e.getMessage() + " line: 209"); e.printStackTrace(); } } } else if (data.getScheme().equals(Constants.FILE)) { logDebug("Dropbox or other DocumentsProvider Uri:" + data); try { parcelFileDescriptor = context.getContentResolver().openFileDescriptor(data, Constants.READ_MODE); FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor(); bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options); parcelFileDescriptor.close(); return bitmap; } catch (IOException e) { logError("Exception:" + e.getMessage() + " line: 223"); e.printStackTrace(); } } else { try { parcelFileDescriptor = context.getContentResolver().openFileDescriptor(data, Constants.READ_MODE); FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor(); bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options); parcelFileDescriptor.close(); return bitmap; } catch (IOException e) { logError("Exception:" + e.getMessage() + " line: 235"); e.printStackTrace(); } } } else { logDebug("Uri for thumbnail:" + data); String[] parts = data.getLastPathSegment().split(":"); String fileId = parts[1]; Cursor cursor = null; try { cursor = context.getContentResolver().query(data, null, null, null, null); if (cursor != null) { logDebug("Cursor size:" + cursor.getCount()); if (cursor.moveToFirst()) { if (data.toString().contains(Constants.VIDEO)) { bitmap = MediaStore.Video.Thumbnails.getThumbnail(context.getContentResolver(), Long.parseLong(fileId), kind, options); } else if (data.toString().contains(Constants.IMAGE)) { bitmap = MediaStore.Images.Thumbnails.getThumbnail(context.getContentResolver(), Long.parseLong(fileId), kind, options); } } } return bitmap; } catch (Exception e) { logError("Exception:" + e.getMessage() + " line: 266"); } finally { if (cursor != null) cursor.close(); } } return bitmap; } }); }
From source file:com.xxxifan.devbox.library.rxfile.RxFile.java
private static Observable<Bitmap> getThumbnailFromUriWithSizeAndKind(final Context context, final Uri data, final int requiredWidth, final int requiredHeight, final int kind) { return Observable.fromCallable(new Func0<Bitmap>() { @Override/* w w w. ja va 2s. c o m*/ public Bitmap call() { Bitmap bitmap = null; ParcelFileDescriptor parcelFileDescriptor; final BitmapFactory.Options options = new BitmapFactory.Options(); if (requiredWidth > 0 && requiredHeight > 0) { options.inJustDecodeBounds = true; options.inSampleSize = calculateInSampleSize(options, requiredWidth, requiredHeight); options.inJustDecodeBounds = false; } if (!isMediaUri(data)) { Log.e(TAG, "Not a media uri"); if (isGoogleDriveDocument(data)) { Log.e(TAG, "Google Drive Uri"); DocumentFile file = DocumentFile.fromSingleUri(context, data); if (file.getType().startsWith(Constants.IMAGE_TYPE) || file.getType().startsWith(Constants.VIDEO_TYPE)) { Log.e(TAG, "Google Drive Uri"); try { parcelFileDescriptor = context.getContentResolver().openFileDescriptor(data, Constants.READ_MODE); FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor(); bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options); parcelFileDescriptor.close(); return bitmap; } catch (IOException e) { e.printStackTrace(); } } } else if (data.getScheme().equals(Constants.FILE)) { Log.e(TAG, "Dropbox or other content provider"); try { parcelFileDescriptor = context.getContentResolver().openFileDescriptor(data, Constants.READ_MODE); FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor(); bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options); parcelFileDescriptor.close(); return bitmap; } catch (IOException e) { e.printStackTrace(); } } else { try { parcelFileDescriptor = context.getContentResolver().openFileDescriptor(data, Constants.READ_MODE); FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor(); bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options); parcelFileDescriptor.close(); return bitmap; } catch (IOException e) { e.printStackTrace(); } } } else { Log.e(TAG, "Uri for thumbnail: " + data.toString()); Log.e(TAG, "Uri for thumbnail: " + data); String[] parts = data.getLastPathSegment().split(":"); String fileId = parts[1]; Cursor cursor = null; try { cursor = context.getContentResolver().query(data, null, null, null, null); if (cursor != null) { Log.e(TAG, "Cursor size: " + cursor.getCount()); if (cursor.moveToFirst()) { if (data.toString().contains(Constants.VIDEO)) { bitmap = MediaStore.Video.Thumbnails.getThumbnail(context.getContentResolver(), Long.parseLong(fileId), kind, options); } else if (data.toString().contains(Constants.IMAGE)) { Log.e(TAG, "Image Uri"); bitmap = MediaStore.Images.Thumbnails.getThumbnail(context.getContentResolver(), Long.parseLong(fileId), kind, options); } Log.e(TAG, bitmap == null ? "null" : "not null"); } } return bitmap; } catch (Exception e) { Log.e(TAG, "Exception while getting thumbnail:" + e.getMessage()); } finally { if (cursor != null) cursor.close(); } } return bitmap; } }); }
From source file:com.github.chenxiaolong.dualbootpatcher.FileUtils.java
@NonNull public static DocumentFile getDocumentFile(@NonNull Context context, @NonNull Uri uri) { DocumentFile df = null;//w w w . j a v a 2 s .c o m if (FILE_SCHEME.equals(uri.getScheme())) { df = DocumentFile.fromFile(new File(uri.getPath())); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && SAF_SCHEME.equals(uri.getScheme()) && SAF_AUTHORITY.equals(uri.getAuthority())) { if (DocumentsContract.isDocumentUri(context, uri)) { df = DocumentFile.fromSingleUri(context, uri); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && DocumentsContract.isTreeUri(uri)) { df = DocumentFile.fromTreeUri(context, uri); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // Best guess is that it's a tree... df = DocumentFile.fromTreeUri(context, uri); } } if (df == null) { throw new IllegalArgumentException("Invalid URI: " + uri); } return df; }
From source file:com.pavlospt.rxfile.RxFile.java
private static File fileFromUri(Context context, Uri data) throws Exception { DocumentFile file = DocumentFile.fromSingleUri(context, data); String fileType = file.getType(); String fileName = file.getName(); File fileCreated;/*from ww w . j a va 2 s . com*/ ParcelFileDescriptor parcelFileDescriptor = context.getContentResolver().openFileDescriptor(data, Constants.READ_MODE); InputStream inputStream = new FileInputStream(parcelFileDescriptor.getFileDescriptor()); logDebug("External cache dir:" + context.getExternalCacheDir()); String filePath = context.getExternalCacheDir() + Constants.FOLDER_SEPARATOR + fileName; String fileExtension = fileName.substring((fileName.lastIndexOf('.')) + 1); String mimeType = getMimeType(fileName); logDebug("From Google Drive guessed type: " + getMimeType(fileName)); logDebug("Extension: " + fileExtension); if (fileType.equals(Constants.APPLICATION_PDF) && mimeType == null) { filePath += "." + Constants.PDF_EXTENSION; } if (!createFile(filePath)) { return new File(filePath); } ReadableByteChannel from = Channels.newChannel(inputStream); WritableByteChannel to = Channels.newChannel(new FileOutputStream(filePath)); fastChannelCopy(from, to); from.close(); to.close(); fileCreated = new File(filePath); logDebug("Path for made file: " + fileCreated.getAbsolutePath()); return fileCreated; }