Google Sample - Orange168/NotesOnReading GitHub Wiki

Sample

#####StorageClient - 21

   / * 
   	* Fires an intent to spin up the "file chooser" UI and select an image.
     */
    public void performFileSearch() {
	// BEGIN_INCLUDE (use_open_document_intent)
	// ACTION_OPEN_DOCUMENT is the intent to choose a file via the system's file browser.
        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
        // Filter to only show results that can be "opened", such as a file (as opposed to a list
        // of contacts or timezones)
        intent.addCategory(Intent.CATEGORY_OPENABLE);

        // Filter to show only images, using the image MIME data type.
        // If one wanted to search for ogg vorbis files, the type would be "audio/ogg".
        // To search for all documents available via installed storage providers, it would be
        // "*/*".
        intent.setType("image/*");
        startActivityForResult(intent, READ_REQUEST_CODE);
        // END_INCLUDE (use_open_document_intent)
    }

####StorageProvider -23 Arrays.xml

<resources>
    <array name="image_res_ids">
        <item>@raw/android_dinner</item>
        <item>@raw/android_rose</item>
        <item>@raw/android_pumpkins_fall</item>
        <item>@raw/android_computer_back</item>
        <item>@raw/android_computer_android_studio</item>
    </array>

    <array name="text_res_ids">
        <item>@raw/cat_names</item>
        <item>@raw/dog_names</item>
    </array>

    <array name="docx_res_ids">
        <item>@raw/example</item>
    </array>
</resources>

Write app raw files to /data/data/<package>/files

//
int[] imageResIds = getResourceIdArray(R.array.image_res_ids);
        for (int resId : imageResIds) {
            writeFileToInternalStorage(resId, ".jpeg");
  }

private int[] getResourceIdArray(int arrayResId) {
        TypedArray ar = getContext().getResources().obtainTypedArray(arrayResId);
        int len = ar.length();
        int[] resIds = new int[len];
        for (int i = 0; i < len; i++) {
            resIds[i] = ar.getResourceId(i, 0);
        }
        ar.recycle();
        return resIds;
}

 private void writeFileToInternalStorage(int resId, String extension) {
        InputStream ins = getContext().getResources().openRawResource(resId);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        int size;
        byte[] buffer = new byte[1024];
        try {
            while ((size = ins.read(buffer, 0, 1024)) >= 0) {
                outputStream.write(buffer, 0, size);
            }
            ins.close();
            buffer = outputStream.toByteArray();
            String filename = getContext().getResources().getResourceEntryName(resId) + extension;
            ///data/data/com.example.android.storageprovider/files
            FileOutputStream fos = getContext().openFileOutput(filename, Context.MODE_PRIVATE);
            fos.write(buffer);
            fos.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Get MimeType

    private static String getTypeForName(String name /*file.getName*/) {
        final int lastDot = name.lastIndexOf('.');
        if (lastDot >= 0) {
            final String extension = name.substring(lastDot + 1);
            final String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
            if (mime != null) {
                return mime;
            }
        }
        return "application/octet-stream";
    }

Get recent use files in specified dir such as /mnt/sdcard

        final File parent = getFileForDocId(rootId); //such as /mnt/sdcard
        // Create a queue to store the most recent documents, which orders by last modified.
        PriorityQueue<File> lastModifiedFiles = new PriorityQueue<File>(5, new Comparator<File>() {
            public int compare(File i, File j) {
                return Long.compare(i.lastModified(), j.lastModified());
            }
        });

        // Iterate through all files and directories in the file structure under the root.  If
        // the file is more recent than the least recently modified, add it to the queue,
        // limiting the number of results.
        final LinkedList<File> pending = new LinkedList<File>();

        // Start by adding the parent to the list of files to be processed
        pending.add(parent);

        // Do while we still have unexamined files
        while (!pending.isEmpty()) {
            // Take a file from the list of unprocessed files
            final File file = pending.removeFirst();
            if (file.isDirectory()) {
                // If it's a directory, add all its children to the unprocessed list
                Collections.addAll(pending, file.listFiles());
            } else {
                // If it's a file, add it to the ordered queue.
                lastModifiedFiles.add(file);
            }
        }
⚠️ **GitHub.com Fallback** ⚠️