Java Code Examples for androidx.documentfile.provider.DocumentFile#fromSingleUri()
The following examples show how to use
androidx.documentfile.provider.DocumentFile#fromSingleUri() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SafUtils.java From SAI with GNU General Public License v3.0 | 6 votes |
@Nullable public static DocumentFile docFileFromSingleUriOrFileUri(Context context, Uri contentUri) { if (ContentResolver.SCHEME_FILE.equals(contentUri.getScheme())) { String path = contentUri.getPath(); if (path == null) return null; File file = new File(path); if (file.isDirectory()) return null; return DocumentFile.fromFile(file); } else { return DocumentFile.fromSingleUri(context, contentUri); } }
Example 2
Source File: LocalBackupUtils.java From SAI with GNU General Public License v3.0 | 6 votes |
@SuppressLint("DefaultLocale") @Nullable private static Uri createBackupUriViaSaf(Context c, Uri backupDirUri, PackageMeta packageMeta, String extension) { DocumentFile backupDirFile = DocumentFile.fromTreeUri(c, backupDirUri); if (backupDirFile == null) return null; String backupFileName = getFileNameForPackageMeta(c, packageMeta); String actualBackupFileName = String.format("%s.%s", backupFileName, extension); int suffix = 0; while (true) { DocumentFile backupFileCandidate = DocumentFile.fromSingleUri(c, SafUtils.buildChildDocumentUri(backupDirUri, actualBackupFileName)); if (backupFileCandidate == null || !backupFileCandidate.exists()) break; actualBackupFileName = String.format("%s(%d).%s", backupFileName, ++suffix, extension); } DocumentFile backupFile = backupDirFile.createFile("saf/sucks", FileUtils.buildValidFatFilename(actualBackupFileName)); if (backupFile == null) return null; return backupFile.getUri(); }
Example 3
Source File: ContentHelper.java From Hentoid with Apache License 2.0 | 6 votes |
/** * Remove the given page from the disk and the DB * * @param image Page to be removed * @param dao DAO to be used */ public static void removePage(@NonNull ImageFile image, @NonNull CollectionDAO dao, @NonNull final Context context) { Helper.assertNonUiThread(); // Remove from DB // NB : start with DB to have a LiveData feedback, because file removal can take much time dao.deleteImageFile(image); // Remove the page from disk if (image.getFileUri() != null && !image.getFileUri().isEmpty()) { Uri uri = Uri.parse(image.getFileUri()); DocumentFile doc = DocumentFile.fromSingleUri(context, uri); if (doc != null && doc.exists()) doc.delete(); } // Update content JSON if it exists (i.e. if book is not queued) Content content = dao.selectContent(image.content.getTargetId()); if (!content.getJsonUri().isEmpty()) updateJson(context, content); }
Example 4
Source File: DCCTransferListAdapter.java From revolution-irc with GNU General Public License v3.0 | 5 votes |
private void delete() { if (mFileUri != null && mFileUri.length() > 0) { Uri uri = Uri.parse(mFileUri); DocumentFile file; if (uri.getScheme().equals("file")) file = DocumentFile.fromFile(new File(uri.getPath())); else file = DocumentFile.fromSingleUri(itemView.getContext(), uri); if (file.exists()) file.delete(); } DCCManager.getInstance(itemView.getContext()).getHistory().removeEntry(mEntryId); }
Example 5
Source File: LibImportDialogFragment.java From Hentoid with Apache License 2.0 | 5 votes |
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); HentoidApp.LifeCycleListener.enable(); // Restores autolock on app going to background @Result int result = processPickerResult(requestCode, resultCode, data); switch (result) { case Result.OK: // File selected DocumentFile doc = DocumentFile.fromSingleUri(requireContext(), selectedFileUri); if (null == doc) return; selectFileBtn.setVisibility(View.GONE); checkFile(doc); break; case Result.CANCELED: Snackbar.make(rootView, R.string.import_canceled, BaseTransientBottomBar.LENGTH_LONG).show(); break; case Result.INVALID_FOLDER: Snackbar.make(rootView, R.string.import_invalid, BaseTransientBottomBar.LENGTH_LONG).show(); break; case Result.OTHER: Snackbar.make(rootView, R.string.import_other, BaseTransientBottomBar.LENGTH_LONG).show(); break; default: // Nothing should happen here } }
Example 6
Source File: ContentHelper.java From Hentoid with Apache License 2.0 | 5 votes |
/** * Update the given Content's JSON file with its current values * * @param context Context to use for the action * @param content Content whose JSON file to update */ public static void updateJson(@NonNull Context context, @NonNull Content content) { Helper.assertNonUiThread(); DocumentFile file = DocumentFile.fromSingleUri(context, Uri.parse(content.getJsonUri())); if (null == file) throw new InvalidParameterException("'" + content.getJsonUri() + "' does not refer to a valid file"); try { JsonHelper.updateJson(context, JsonContent.fromEntity(content), JsonContent.class, file); } catch (IOException e) { Timber.e(e, "Error while writing to %s", content.getJsonUri()); } }
Example 7
Source File: AttachmentHandler.java From tindroid with Apache License 2.0 | 4 votes |
@NonNull static FileDetails getFileDetails(@NonNull final Context context, @NonNull Uri uri, @Nullable String filePath) { final ContentResolver resolver = context.getContentResolver(); String fname = null; long fsize = 0L; int orientation = -1; FileDetails result = new FileDetails(); String mimeType = resolver.getType(uri); if (mimeType == null) { mimeType = UiUtils.getMimeType(uri); } result.mimeType = mimeType; String[] projection; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { projection = new String[]{OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE, MediaStore.MediaColumns.ORIENTATION}; } else { projection = new String[]{OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE}; } try (Cursor cursor = resolver.query(uri, projection, null, null, null)) { if (cursor != null && cursor.moveToFirst()) { fname = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)); fsize = cursor.getLong(cursor.getColumnIndex(OpenableColumns.SIZE)); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { int idx = cursor.getColumnIndex(MediaStore.MediaColumns.ORIENTATION); if (idx >= 0) { orientation = cursor.getInt(idx); } } } } // In degrees. result.imageOrientation = orientation; // Still no size? Try opening directly. if (fsize <= 0 || orientation < 0) { String path = filePath != null ? filePath : UiUtils.getContentPath(context, uri); if (path != null) { result.filePath = path; File file = new File(path); if (fname == null) { fname = file.getName(); } fsize = file.length(); } else { try { DocumentFile df = DocumentFile.fromSingleUri(context, uri); if (df != null) { fname = df.getName(); fsize = df.length(); } } catch (SecurityException ignored) {} } } result.fileName = fname; result.fileSize = fsize; return result; }
Example 8
Source File: ImagePagerAdapter.java From Hentoid with Apache License 2.0 | 4 votes |
@Override public synchronized Reader obtain() throws IOException { DocumentFile file = DocumentFile.fromSingleUri(HentoidApp.getInstance().getApplicationContext(), uri); if (null == file || !file.exists()) return null; // Not triggered return new ImgReader(file.getUri()); }
Example 9
Source File: FileHelper.java From Hentoid with Apache License 2.0 | 4 votes |
public static DocumentFile getFileFromUriString(@NonNull final Context context, @NonNull final String uriStr) { Uri fileUri = Uri.parse(uriStr); return DocumentFile.fromSingleUri(context, fileUri); }
Example 10
Source File: FileInfoDialog.java From turbo-editor with GNU General Public License v3.0 | 4 votes |
@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")); } // 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(); }