Java Code Examples for androidx.documentfile.provider.DocumentFile#fromFile()

The following examples show how to use androidx.documentfile.provider.DocumentFile#fromFile() . 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: MimiUtil.java    From mimi-reader with Apache License 2.0 6 votes vote down vote up
private static DocumentFile directoryToDocumentFile(final Context context, final String dir) {
    if (!TextUtils.isEmpty(dir)) {
        try {
            if (dir.startsWith(Utils.SCHEME_CONTENT)) {
                return DocumentFile.fromTreeUri(context, Uri.parse(dir));
            } else {
                return DocumentFile.fromFile(new File(dir));
            }
        } catch (Exception e) {
            Log.e(LOG_TAG, "Error creating DocumentFile from " + dir, e);
            return null;
        }
    } else {
        DocumentFile defaultDir = DocumentFile.fromFile(new File(getPicturesDirectoryAsFile(), "/Mimi"));
        if (!defaultDir.exists()) {
            DocumentFile externalStorageDir = getPicturesDirectory();
            DocumentFile mimiFolder = externalStorageDir.createDirectory("Mimi");
        }
        return defaultDir;
    }
}
 
Example 2
Source File: BookSourcePresenter.java    From a with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void importBookSourceLocal(String path) {
    if (TextUtils.isEmpty(path)) {
        mView.toast(R.string.read_file_error);
        return;
    }
    String json;
    DocumentFile file;
    try {
        file = DocumentFile.fromFile(new File(path));
    } catch (Exception e) {
        mView.toast(path + "无法打开!");
        return;
    }
    json = DocumentHelper.readString(file);
    if (!isEmpty(json)) {
        mView.showSnackBar("正在导入书源", Snackbar.LENGTH_INDEFINITE);
        importBookSource(json);
    } else {
        mView.toast(R.string.read_file_error);
    }
}
 
Example 3
Source File: BookSourcePresenter.java    From MyBookshelf with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void importBookSourceLocal(String path) {
    if (TextUtils.isEmpty(path)) {
        mView.toast(R.string.read_file_error);
        return;
    }
    String json;
    DocumentFile file;
    try {
        file = DocumentFile.fromFile(new File(path));
    } catch (Exception e) {
        mView.toast(path + "无法打开!");
        return;
    }
    json = DocumentHelper.readString(file);
    if (!isEmpty(json)) {
        mView.showSnackBar("正在导入书源", Snackbar.LENGTH_INDEFINITE);
        importBookSource(json);
    } else {
        mView.toast(R.string.read_file_error);
    }
}
 
Example 4
Source File: SafUtils.java    From SAI with GNU General Public License v3.0 6 votes vote down vote up
@Nullable
public static DocumentFile docFileFromTreeUriOrFileUri(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.fromTreeUri(context, contentUri);
    }
}
 
Example 5
Source File: DocumentUtil.java    From MyBookshelf with GNU General Public License v3.0 5 votes vote down vote up
public static DocumentFile createDirIfNotExist(Context context, Uri rootUri, String... subDirs) {
    DocumentFile root;
    if ("content".equals(rootUri.getScheme()))
        root = DocumentFile.fromTreeUri(context, rootUri);
    else
        root = DocumentFile.fromFile(new File(rootUri.getPath()));
    return createDirIfNotExist(root, subDirs);
}
 
Example 6
Source File: DocumentUtil.java    From HaoReader with GNU General Public License v3.0 5 votes vote down vote up
public static DocumentFile getDirDocument(Context context, Uri rootUri, String... subDirs) {
    DocumentFile root;
    if ("content".equals(rootUri.getScheme()))
        root = DocumentFile.fromTreeUri(context, rootUri);
    else
        root = DocumentFile.fromFile(new File(rootUri.getPath()));
    return getDirDocument(root, subDirs);
}
 
Example 7
Source File: DocumentUtil.java    From HaoReader with GNU General Public License v3.0 5 votes vote down vote up
public static boolean deleteFile(Context context, String fileName, Uri rootUri, String... subDirs) {
    DocumentFile root;
    if ("content".equals(rootUri.getScheme()))
        root = DocumentFile.fromTreeUri(context, rootUri);
    else
        root = DocumentFile.fromFile(new File(rootUri.getPath()));
    return deleteFile(fileName, root, subDirs);
}
 
Example 8
Source File: DocumentUtil.java    From HaoReader with GNU General Public License v3.0 5 votes vote down vote up
public static DocumentFile createDirIfNotExist(Context context, Uri rootUri, String... subDirs) {
    DocumentFile root;
    if ("content".equals(rootUri.getScheme()))
        root = DocumentFile.fromTreeUri(context, rootUri);
    else
        root = DocumentFile.fromFile(new File(rootUri.getPath()));
    return createDirIfNotExist(root, subDirs);
}
 
Example 9
Source File: DocumentUtil.java    From HaoReader with GNU General Public License v3.0 5 votes vote down vote up
public static boolean isFileExist(Context context, String fileName, Uri rootUri, String... subDirs) {
    DocumentFile root;
    if ("content".equals(rootUri.getScheme()))
        root = DocumentFile.fromTreeUri(context, rootUri);
    else
        root = DocumentFile.fromFile(new File(rootUri.getPath()));
    return isFileExist(fileName, root, subDirs);
}
 
Example 10
Source File: ReplaceRulePresenter.java    From MyBookshelf with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void importDataSLocal(String path) {
    String json;
    DocumentFile file = DocumentFile.fromFile(new File(path));
    json = DocumentHelper.readString(file);
    if (!isEmpty(json)) {
        importDataS(json);
    } else {
        mView.toast("文件读取失败");
    }
}
 
Example 11
Source File: DocumentUtil.java    From MyBookshelf with GNU General Public License v3.0 5 votes vote down vote up
public static DocumentFile getDirDocument(Context context, Uri rootUri, String... subDirs) {
    DocumentFile root;
    if ("content".equals(rootUri.getScheme()))
        root = DocumentFile.fromTreeUri(context, rootUri);
    else
        root = DocumentFile.fromFile(new File(rootUri.getPath()));
    return getDirDocument(root, subDirs);
}
 
Example 12
Source File: DocumentUtil.java    From MyBookshelf with GNU General Public License v3.0 5 votes vote down vote up
public static boolean deleteFile(Context context, String fileName, Uri rootUri, String... subDirs) {
    DocumentFile root;
    if ("content".equals(rootUri.getScheme()))
        root = DocumentFile.fromTreeUri(context, rootUri);
    else
        root = DocumentFile.fromFile(new File(rootUri.getPath()));
    return deleteFile(fileName, root, subDirs);
}
 
Example 13
Source File: DocumentUtil.java    From a with GNU General Public License v3.0 5 votes vote down vote up
public static boolean isFileExist(Context context, String fileName, Uri rootUri, String... subDirs) {
    DocumentFile root;
    if ("content".equals(rootUri.getScheme()))
        root = DocumentFile.fromTreeUri(context, rootUri);
    else
        root = DocumentFile.fromFile(new File(rootUri.getPath()));
    return isFileExist(fileName, root, subDirs);
}
 
Example 14
Source File: DCCTransferListAdapter.java    From revolution-irc with GNU General Public License v3.0 5 votes vote down vote up
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 15
Source File: TxtChapterRulePresenter.java    From a with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void importDataSLocal(String path) {
    String json;
    DocumentFile file = DocumentFile.fromFile(new File(path));
    json = DocumentHelper.readString(file);
    if (!isEmpty(json)) {
        importDataS(json);
    } else {
        mView.toast("文件读取失败");
    }
}
 
Example 16
Source File: DocumentUtil.java    From a with GNU General Public License v3.0 5 votes vote down vote up
public static DocumentFile getDirDocument(Context context, Uri rootUri, String... subDirs) {
    DocumentFile root;
    if ("content".equals(rootUri.getScheme()))
        root = DocumentFile.fromTreeUri(context, rootUri);
    else
        root = DocumentFile.fromFile(new File(rootUri.getPath()));
    return getDirDocument(root, subDirs);
}
 
Example 17
Source File: DocumentUtil.java    From a with GNU General Public License v3.0 5 votes vote down vote up
public static boolean deleteFile(Context context, String fileName, Uri rootUri, String... subDirs) {
    DocumentFile root;
    if ("content".equals(rootUri.getScheme()))
        root = DocumentFile.fromTreeUri(context, rootUri);
    else
        root = DocumentFile.fromFile(new File(rootUri.getPath()));
    return deleteFile(fileName, root, subDirs);
}
 
Example 18
Source File: DocumentUtil.java    From a with GNU General Public License v3.0 5 votes vote down vote up
public static DocumentFile createDirIfNotExist(Context context, Uri rootUri, String... subDirs) {
    DocumentFile root;
    if ("content".equals(rootUri.getScheme()))
        root = DocumentFile.fromTreeUri(context, rootUri);
    else
        root = DocumentFile.fromFile(new File(rootUri.getPath()));
    return createDirIfNotExist(root, subDirs);
}
 
Example 19
Source File: MimiUtil.java    From mimi-reader with Apache License 2.0 4 votes vote down vote up
public static DocumentFile getPicturesDirectory() {
    return DocumentFile.fromFile(getPicturesDirectoryAsFile());
}
 
Example 20
Source File: FileInfoDialog.java    From turbo-editor with GNU General Public License v3.0 4 votes vote down vote up
@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();
}