Example usage for android.support.v4.provider DocumentFile getParentFile

List of usage examples for android.support.v4.provider DocumentFile getParentFile

Introduction

In this page you can find the example usage for android.support.v4.provider DocumentFile getParentFile.

Prototype

public DocumentFile getParentFile() 

Source Link

Usage

From source file:com.kanedias.vanilla.lyrics.LyricsShowActivity.java

/**
 * Write lyrics to *.lrc file through SAF framework - the only way to do it in Android > 4.4 when working with SD card
 *//*from ww w . ja  v  a  2s. c o  m*/
private void writeThroughSaf(byte[] data, File original, String name) {
    DocumentFile originalRef;
    if (mPrefs.contains(PREF_SDCARD_URI)) {
        // no sorcery can allow you to gain URI to the document representing file you've been provided with
        // you have to find it again now using Document API

        // /storage/volume/Music/some.mp3 will become [storage, volume, music, some.mp3]
        List<String> pathSegments = new ArrayList<>(Arrays.asList(original.getAbsolutePath().split("/")));
        Uri allowedSdRoot = Uri.parse(mPrefs.getString(PREF_SDCARD_URI, ""));
        originalRef = findInDocumentTree(DocumentFile.fromTreeUri(this, allowedSdRoot), pathSegments);
    } else {
        // user will click the button again
        return;
    }

    if (originalRef == null || originalRef.getParentFile() == null) {
        // nothing selected or invalid file?
        Toast.makeText(this, R.string.saf_nothing_selected, Toast.LENGTH_LONG).show();
        return;
    }

    DocumentFile lrcFileRef = originalRef.getParentFile().createFile("image/*", name);
    if (lrcFileRef == null) {
        // couldn't create file?
        Toast.makeText(this, R.string.saf_write_error, Toast.LENGTH_LONG).show();
        return;
    }

    try {
        ParcelFileDescriptor pfd = getContentResolver().openFileDescriptor(lrcFileRef.getUri(), "rw");
        if (pfd == null) {
            // should not happen
            Log.e(LOG_TAG, "SAF provided incorrect URI!" + lrcFileRef.getUri());
            return;
        }

        FileOutputStream fos = new FileOutputStream(pfd.getFileDescriptor());
        fos.write(data);
        fos.close();

        // rescan original file
        Toast.makeText(this, R.string.file_written_successfully, Toast.LENGTH_SHORT).show();
    } catch (Exception e) {
        Toast.makeText(this, getString(R.string.saf_write_error) + e.getLocalizedMessage(), Toast.LENGTH_LONG)
                .show();
        Log.e(LOG_TAG, "Failed to write to file descriptor provided by SAF!", e);
    }
}

From source file:com.almalence.plugins.capture.video.VideoCapturePlugin.java

/**
 * Appends mp4 audio/video from {@code anotherFileDescriptor} to
 * {@code mainFileDescriptor}./*from   w  w  w .  j av a  2s.co  m*/
 */
public static DocumentFile appendNew(DocumentFile[] inputFiles) {
    try {
        DocumentFile targetFile = inputFiles[0];
        int[] inputFilesFds = new int[inputFiles.length];
        ArrayList<ParcelFileDescriptor> pfdsList = new ArrayList<ParcelFileDescriptor>();

        int i = 0;
        for (DocumentFile f : inputFiles) {
            ParcelFileDescriptor pfd = ApplicationScreen.instance.getContentResolver()
                    .openFileDescriptor(f.getUri(), "rw");
            pfdsList.add(pfd);
            inputFilesFds[i] = pfd.getFd();
            i++;
        }

        if (targetFile.exists() && targetFile.length() > 0) {
            String tmpFileName = targetFile.getName() + ".tmp";
            DocumentFile tmpTargetFile = targetFile.getParentFile().createFile("video/mp4", tmpFileName);
            ParcelFileDescriptor targetFilePfd = ApplicationScreen.instance.getContentResolver()
                    .openFileDescriptor(tmpTargetFile.getUri(), "rw");

            Mp4Editor.appendFds(inputFilesFds, targetFilePfd.getFd());

            targetFilePfd.close();
            for (ParcelFileDescriptor pfd : pfdsList) {
                pfd.close();
            }

            return tmpTargetFile;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}