Courtesy: GitHub

Android StorageProvider Sample

This sample shows how to implement a simple documents provider using the storage access framework available in Android 4.4.

Introduction

This sample uses the StorageAccessFramework introduced in Android 4.4 to implement a DocumentsProvider.

See Writing A Custom Document Provider guide for all the details on how to do this.

Pre-requisites

  • Android SDK 26
  • Android Build Tools v26.0.1
  • Android Support Repository

Screenshots

Screenshot Screenshot

Getting Started

This sample uses the Gradle build system. To build this project, use the "gradlew build" command or use "Import Project" in Android Studio.

Support

If you've found an error in this sample, please file an issue: https://github.com/googlesamples/android-StorageProvider

Patches are encouraged, and may be submitted by forking this project and submitting a pull request through GitHub. Please see CONTRIBUTING.md for more details.

License

Copyright 2017 The Android Open Source Project, Inc.

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

compile "com.android.support:support-v4:26.1.0"

compile "com.android.support:support-v13:26.1.0"

compile "com.android.support:cardview-v7:26.1.0"

compile "com.android.support:appcompat-v7:26.1.0"

compileSdkVersion 26

minSdkVersion 19

targetSdkVersion 22

package com.example.android.storageprovider

versionCode 1

versionName 1.0

uses-permission

  • android.permission.READ_EXTERNAL_STORAGE
  • android.permission.WRITE_EXTERNAL_STORAGE

MainActivity

A simple launcher activity containing a summary sample description and a few action bar buttons.

Create a chain of targets that will receive log data

MyCloudProvider

Manages documents and exposes them to the Android system for sharing.

@param projection the requested root column projection @return either the requested root column projection, or the default projection if the requested projection is null.

Get a file's MIME type * @param file the File object whose type we want @return the MIME type of the file

Get the MIME data type of a document, given its filename. * @param name the filename of the document @return the MIME data type of a document

Gets a string of unique MIME data types a directory supports, separated by newlines. This should not change. * @param parent the File for the parent directory @return a string of the unique MIME data types the parent directory supports

Get the document ID given a File. The document id must be consistent across time. Other applications may save the ID and use it to reference documents later.

This implementation is specific to this demo. It assumes only one root and is built directly from the file structure. However, it is possible for a document to be a child of multiple directories (for example "android" and "images"), in which case the file must have the same consistent, unique document ID in both cases. * @param file the File whose document ID you want @return the corresponding document ID

Add a representation of a file to a cursor. * @param result the cursor to modify @param docId the document ID representing the desired file (may be null if given file) @param file the File object representing the desired file (may be null if given docID) @throws java.io.FileNotFoundException

Translate your custom URI scheme into a File object. * @param docId the document ID representing the desired file @return a File represented by the given document ID @throws java.io.FileNotFoundException

Preload sample files packaged in the apk into the internal storage directory. This is a dummy function specific to this demo. The MyCloud mock cloud service doesn't actually have a backend, so it simulates by reading content from the device's internal storage.

Write a file to internal storage. Used to set up our dummy "cloud server". * @param resId the resource ID of the file to write to internal storage @param extension the file extension (ex. .png, .mp3)

Dummy function to determine whether the user is logged in.