Skip to content

Latest commit

 

History

History
138 lines (125 loc) · 4.43 KB

README.md

File metadata and controls

138 lines (125 loc) · 4.43 KB

Scoper - Access any file with zero permission required

Fakhruls's GitHub statsFakhruls's GitHub stats

With this library you will able to access file from your mobile without requiring any permissions including READ_EXTERNAL_STORAGE or WRITE_EXTERNAL_STORAGE which are restricted in Android 11 and beyond for only File managing related applications.

To start with, Copy or clone this repository 1.0.0 release branch into your project, Then just call openFileIntent() for accessing documents and manage your documents, files in startActivityForResult or use ActivityResultContracts.StartActivityForResult()

function openGallery() {
    intentDocument.launch(openFileIntent(null))
}

To store the file into your app cache and use it later anywhere,

var file:File? =  FileUtil.from(this,result.data?.data!!)

Then manage it according to your need,

    private var intentDocument =
        registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
            if (result.resultCode == Activity.RESULT_OK && result.data != null) {

                var file:File? =  FileUtil.from(this,result.data?.data!!)
	
                    if (result.resultCode == Activity.RESULT_OK) {
                       /**
                       * Your code here for managing the file
                       */
                }
            }
        }

Request specefic filetype

You can request specefic file type according to your preference,

    private fun getDocument() {
        intentDocument.launch(
            openFileIntent(
                listOf(
                    FileType.PDF,
                    FileType.IMAGE
                )
            )
        )
    }

Then manage it according to your need,

    private var intentDocument =
        registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
            if (result.resultCode == Activity.RESULT_OK && result.data != null) {

                var file:File? =  FileUtil.from(this,result.data?.data!!)
                if(file?.extension.equals("pdf",ignoreCase = true)){
                    if (result.resultCode == Activity.RESULT_OK) {
                       /**
                       * Your code here for file type
                       */
                    }
                }else{
                     /**
                       * Your code here for image type
                       */
                }

            }
        }

Add provider in Application scope of your manifest and provider_paths to res/xml in order work under your application scope.

        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.fileProvider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
        </provider>

and create an XML file into your res/xml named provider_paths

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-cache-path
        name="external_cache_files"
        path="." />
    <external-files-path
        name="external_files_files"
        path="." />
    <external-path
        name="external_files"
        path="." />
</paths>

File operation's

To get the cached file name,

file.name

To get the cached file path,

file.path

To get the cached file absolute path,

file.absolutePath

To get the cached file Uri,

file.toURI()

To get the cached file size in KB,

file?.length()!! / 1024

License

Apache Version 2.0

Copyright (C) 2021 Fakhrul Siddiqei

Licensed 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.