- lopspower / README.md
- Environment
- Class Overview
- Summary
- Constants
- public static final String MEDIA_BAD_REMOVAL
- public static final String MEDIA_CHECKING
- public static final String MEDIA_MOUNTED
- public static final String MEDIA_MOUNTED_READ_ONLY
- public static final String MEDIA_NOFS
- public static final String MEDIA_REMOVED
- public static final String MEDIA_SHARED
- public static final String MEDIA_UNMOUNTABLE
- public static final String MEDIA_UNMOUNTED
- Fields
- public static String DIRECTORY_ALARMS
- public static String DIRECTORY_DCIM
- public static String DIRECTORY_DOWNLOADS
- public static String DIRECTORY_MOVIES
- public static String DIRECTORY_MUSIC
- public static String DIRECTORY_NOTIFICATIONS
- public static String DIRECTORY_PICTURES
- public static String DIRECTORY_PODCASTS
- public static String DIRECTORY_RINGTONES
- Public Constructors
- public Environment ()
- Public Methods
- public static File getDataDirectory ()
- public static File getDownloadCacheDirectory ()
- public static File getExternalStorageDirectory ()
- public static File getExternalStoragePublicDirectory (String type)
- Kotlin Android DownloadManager Tutorial and Examples
- What is DownloadManager?
- Why Download?
- Common Questions
- Which Types of Downloads are Handled by DownloadManager
- Where Do the Downloads take Place?
- Why Use the DownloadManager?
- DownloadManager Quick HowTos and Snippets
- DownloadManager Examples
- Example 1. Download File, View All Download, Delete Download
- Video Tutorial
- OverView of the App
- Example 2: Kotlin Android simple DownloadManager Example
- Step 1: Create Project
- Step 2: Dependencies
- Step 3: Design Layout
- Step 4: Write Code
- Step 5: Add permissions
- Reference
- Example 3: Kotlin Android – Dynamic Download
- Step 1: Create Project
- Step 2: Dependencies
- Step 3: Design Layout
- Step 4: Write Code
- Android Manifest
- Reference
- Example 4: Kotlin DownloadManager with Runtime Permissions and Coroutines
- Step 1: Create Project
- Step 2: Dependencies
- Step 3: Design Layout
- Step 4: Write Code
- Reference
- Oclemy
lopspower / README.md
All Android Directory Path
1) System directories
⚠️ We can’t write to these folers
Method | Result |
---|---|
Environment.getDataDirectory() | /data |
Environment.getDownloadCacheDirectory() | /cache |
Environment.getRootDirectory() | /system |
2) External storage directories
⚠️ Need WRITE_EXTERNAL_STORAGE Permission
Method | Result |
---|---|
Environment.getExternalStorageDirectory() | /storage/sdcard0 |
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_ALARMS) | /storage/sdcard0/Alarms |
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) | /storage/sdcard0/DCIM |
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) | /storage/sdcard0/Download |
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES) | /storage/sdcard0/Movies |
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC) | /storage/sdcard0/Music |
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_NOTIFICATIONS) | /storage/sdcard0/Notifications |
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) | /storage/sdcard0/Pictures |
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PODCASTS) | /storage/sdcard0/Podcasts |
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_RINGTONES) | /storage/sdcard0/Ringtones |
3) Application directories
Method | Result |
---|---|
getCacheDir() | /data/data/package/cache |
getFilesDir() | /data/data/package/files |
getFilesDir().getParent() | /data/data/package |
4) Application External storage directories
Источник
Environment
Class Overview
Provides access to environment variables.
Summary
Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
String | MEDIA_BAD_REMOVAL | getExternalStorageState() returns MEDIA_BAD_REMOVAL if the media was removed before it was unmounted. | |||||||||
String | MEDIA_CHECKING | getExternalStorageState() returns MEDIA_CHECKING if the media is present and being disk-checked | |||||||||
String | MEDIA_MOUNTED | getExternalStorageState() returns MEDIA_MOUNTED if the media is present and mounted at its mount point with read/write access. | |||||||||
String | MEDIA_MOUNTED_READ_ONLY | getExternalStorageState() returns MEDIA_MOUNTED_READ_ONLY if the media is present and mounted at its mount point with read only access. | |||||||||
String | MEDIA_NOFS | getExternalStorageState() returns MEDIA_NOFS if the media is present but is blank or is using an unsupported filesystem | |||||||||
String | MEDIA_REMOVED | getExternalStorageState() returns MEDIA_REMOVED if the media is not present. | |||||||||
String | MEDIA_SHARED | getExternalStorageState() returns MEDIA_SHARED if the media is present not mounted, and shared via USB mass storage. | |||||||||
String | MEDIA_UNMOUNTABLE | getExternalStorageState() returns MEDIA_UNMOUNTABLE if the media is present but cannot be mounted. | |||||||||
String | MEDIA_UNMOUNTED | getExternalStorageState() returns MEDIA_UNMOUNTED if the media is present but not mounted. |
Fields | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
DIRECTORY_ALARMS | Standard directory in which to place any audio files that should be in the list of alarms that the user can select (not as regular music). | ||||||||||
DIRECTORY_DCIM | The traditional location for pictures and videos when mounting the device as a camera. | ||||||||||
DIRECTORY_DOWNLOADS | Standard directory in which to place files that have been downloaded by the user. | ||||||||||
DIRECTORY_MOVIES | Standard directory in which to place movies that are available to the user. | ||||||||||
DIRECTORY_MUSIC | Standard directory in which to place any audio files that should be in the regular list of music for the user. | ||||||||||
DIRECTORY_NOTIFICATIONS | Standard directory in which to place any audio files that should be in the list of notifications that the user can select (not as regular music). | ||||||||||
DIRECTORY_PICTURES | Standard directory in which to place pictures that are available to the user. | ||||||||||
DIRECTORY_PODCASTS | Standard directory in which to place any audio files that should be in the list of podcasts that the user can select (not as regular music). | ||||||||||
DIRECTORY_RINGTONES | Standard directory in which to place any audio files that should be in the list of ringtones that the user can select (not as regular music). |
Public Constructors |
---|
Public Methods | |||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Constantspublic static final String MEDIA_BAD_REMOVALgetExternalStorageState() returns MEDIA_BAD_REMOVAL if the media was removed before it was unmounted. public static final String MEDIA_CHECKINGgetExternalStorageState() returns MEDIA_CHECKING if the media is present and being disk-checked public static final String MEDIA_MOUNTEDgetExternalStorageState() returns MEDIA_MOUNTED if the media is present and mounted at its mount point with read/write access. public static final String MEDIA_MOUNTED_READ_ONLYgetExternalStorageState() returns MEDIA_MOUNTED_READ_ONLY if the media is present and mounted at its mount point with read only access. public static final String MEDIA_NOFSgetExternalStorageState() returns MEDIA_NOFS if the media is present but is blank or is using an unsupported filesystem public static final String MEDIA_REMOVEDgetExternalStorageState() returns MEDIA_REMOVED if the media is not present. public static final String MEDIA_SHAREDgetExternalStorageState() returns MEDIA_SHARED if the media is present not mounted, and shared via USB mass storage. public static final String MEDIA_UNMOUNTABLEgetExternalStorageState() returns MEDIA_UNMOUNTABLE if the media is present but cannot be mounted. Typically this happens if the file system on the media is corrupted. public static final String MEDIA_UNMOUNTEDgetExternalStorageState() returns MEDIA_UNMOUNTED if the media is present but not mounted. Fieldspublic static String DIRECTORY_ALARMSStandard directory in which to place any audio files that should be in the list of alarms that the user can select (not as regular music). This may be combined with DIRECTORY_MUSIC , DIRECTORY_PODCASTS , DIRECTORY_NOTIFICATIONS , and DIRECTORY_RINGTONES as a series of directories to categories a particular audio file as more than one type. public static String DIRECTORY_DCIMThe traditional location for pictures and videos when mounting the device as a camera. Note that this is primarily a convention for the top-level public directory, as this convention makes no sense elsewhere. public static String DIRECTORY_DOWNLOADSStandard directory in which to place files that have been downloaded by the user. Note that this is primarily a convention for the top-level public directory, you are free to download files anywhere in your own private directories. Also note that though the constant here is named DIRECTORY_DOWNLOADS (plural), the actual file name is non-plural for backwards compatibility reasons. public static String DIRECTORY_MOVIESStandard directory in which to place movies that are available to the user. Note that this is primarily a convention for the top-level public directory, as the media scanner will find and collect movies in any directory. public static String DIRECTORY_MUSICStandard directory in which to place any audio files that should be in the regular list of music for the user. This may be combined with DIRECTORY_PODCASTS , DIRECTORY_NOTIFICATIONS , DIRECTORY_ALARMS , and DIRECTORY_RINGTONES as a series of directories to categories a particular audio file as more than one type. public static String DIRECTORY_NOTIFICATIONSStandard directory in which to place any audio files that should be in the list of notifications that the user can select (not as regular music). This may be combined with DIRECTORY_MUSIC , DIRECTORY_PODCASTS , DIRECTORY_ALARMS , and DIRECTORY_RINGTONES as a series of directories to categories a particular audio file as more than one type. public static String DIRECTORY_PICTURESStandard directory in which to place pictures that are available to the user. Note that this is primarily a convention for the top-level public directory, as the media scanner will find and collect pictures in any directory. public static String DIRECTORY_PODCASTSStandard directory in which to place any audio files that should be in the list of podcasts that the user can select (not as regular music). This may be combined with DIRECTORY_MUSIC , DIRECTORY_NOTIFICATIONS , DIRECTORY_ALARMS , and DIRECTORY_RINGTONES as a series of directories to categories a particular audio file as more than one type. public static String DIRECTORY_RINGTONESStandard directory in which to place any audio files that should be in the list of ringtones that the user can select (not as regular music). This may be combined with DIRECTORY_MUSIC , DIRECTORY_PODCASTS , DIRECTORY_NOTIFICATIONS , and DIRECTORY_ALARMS as a series of directories to categories a particular audio file as more than one type. Public Constructorspublic Environment ()Public Methodspublic static File getDataDirectory ()Gets the Android data directory. public static File getDownloadCacheDirectory ()Gets the Android download/cache content directory. public static File getExternalStorageDirectory ()Gets the Android external storage directory. This directory may not currently be accessible if it has been mounted by the user on their computer, has been removed from the device, or some other problem has happened. You can determine its current state with getExternalStorageState() . Note: don’t be confused by the word «external» here. This directory can better be thought as media/shared storage. It is a filesystem that can hold a relatively large amount of data and that is shared across all applications (does not enforce permissions). Traditionally this is an SD card, but it may also be implemented as built-in storage in a device that is distinct from the protected internal storage and can be mounted as a filesystem on a computer. On devices with multiple users (as described by UserManager ), each user has their own isolated external storage. Applications only have access to the external storage for the user they’re running as. In devices with multiple «external» storage directories (such as both secure app storage and mountable shared storage), this directory represents the «primary» external storage that the user will interact with. Applications should not directly use this top-level directory, in order to avoid polluting the user’s root namespace. Any files that are private to the application should be placed in a directory returned by Context.getExternalFilesDir , which the system will take care of deleting if the application is uninstalled. Other shared files should be placed in one of the directories returned by getExternalStoragePublicDirectory(String) . Writing to this path requires the WRITE_EXTERNAL_STORAGE permission. In a future platform release, access to this path will require the READ_EXTERNAL_STORAGE permission, which is automatically granted if you hold the write permission. This path may change between platform versions, so applications should only persist relative paths. Here is an example of typical code to monitor the state of external storage: See Alsopublic static File getExternalStoragePublicDirectory (String type)Get a top-level public external storage directory for placing files of a particular type. This is where the user will typically place and manage their own files, so you should be careful about what you put here to ensure you don’t erase their files or get in the way of their own organization. On devices with multiple users (as described by UserManager ), each user has their own isolated external storage. Applications only have access to the external storage for the user they’re running as. Here is an example of typical code to manipulate a picture on the public external storage: Источник Kotlin Android DownloadManager Tutorial and ExamplesAndroid DownloadManager Tutorial and Examples In this session we explore the android.app.DownloadManager class, how to use it and why it’s important.We will look at issues like the types of downloads you can make with DownloadManager class, how to make the actual requests, how to show progress in status bar via noification, open downloaded file and even remove. What is DownloadManager?The DownloadManager is a system service which we can use to handle long running downloads. Why Download?Don’t underestimate the need for downloads and how complex it can get. The state of bandwith in the current era implies that we are still not able, at least in many parts of the world, to be able to obtain files we need on demand any time. Users don’t leave their internet on all time as it does cost money and drain the device’s battery. Hence being able to download files and store them locally is important. But this is not an easy task to implement properly. Especially doing it correctly and efficiently. Yet it is one of those tasks that can absolutely be shared among apps. There is no real need most of the time to re-invent the wheel while making http downloads. It makes sense to have one simple to use class that can do this efficiently and inform our app when the task is complete. Being able to download data is powerful way of enriching our apps as we can get files from the internet which our app can then use. If the user happens to remove the file, we can re-download it again. Common QuestionsHere are some of the questions to allow us understand the DownloadManager class. Which Types of Downloads are Handled by DownloadManagerNormally there are several protocols for communications across devices. And certainly download a file is just a form or communicating between atleast two devices. One device supplying a file while another receiving it. Beware the DownloadManager is used to handle only HTTP downloads. DownloadManager is especially useful if your downlaods are long running. Where Do the Downloads take Place?Where with regards to threads. Well the downloads will definitely take place in the background thread. Why Use the DownloadManager?Well what are it’s advantages? Well we have several. For example
DownloadManager Quick HowTos and Snippets1. How to download videos using DownloadManager classWe want to download videos using the DownloadManager class in android. We want to give them appropriate names and store them in external storage. First we have been passed the video url as a parameter. Let’ say we want to download only mp4 files, so we provide a simple boolean check to check if it contains mp4. Maybe you could use endsWith() if you like instead of contains() . DownloadManager ExamplesLet’s look at some DownloadManager examples. Example 1. Download File, View All Download, Delete DownloadIn this complete example we want to see how to download a file from the internet using the downloadManager class. Then we can open the donwload by clicking the notification in the system bar or from internally in our app. Moreover we can delete the file, view all downloads etc. Video TutorialHere we have a video tutorial for this example. Here’s the demo of the app. OverView of the App
(a). MainActivity.javaMainActivity as you can imagine is our launcher activity. it will derive from the AppCompatActivity . This is where we write all our code. But first we start by making several imports. These include:
HowTo’s Let’s look at several howTos. (a). How to Initialize the DownloadManager As a System service, the DownloadManager is not instantiated directly. Instead we will initialize it using the getSystemService() method and cast the resultant object into the DownloadManager class. (b). How to Create a Download Request Inside the DownloadManager class is an inner class called the Request class. We can use this class to define our HTTP Request. We conveniently do this via the builder patter. Clearly you can see we’ve pased the Uri into our Request constructor. Then set the title, descrription, destination and notification visibility. (c). How to enqueue a Download Enqueueing a download means adding it to the download queue of the download manager. Then the queue will be processed automatically the system. You enqueue a download using the enqueue() method. This will return us a download id. (d). How to remove/delete downloaded file Well you can remove a downloaded file from the download manager using the remove() method of the DownloadManager class. You pass the download id. Here’s the full source code. (b). activity_main.xmlWe need a layout for our MainActivity . Here’s the code. (c). AndroidManifest.xmlIn our AndroidManifest we need to add permissions for internet connectivity as well as for writing to external storage. This is because we download our image from internet and write to our external storage. Here’s my full AndroidManifest.xml file: DownloadHere are reference resources:
Example 2: Kotlin Android simple DownloadManager ExampleA simple open source Kotlin downloadmanager example suitable for absolute beginners. Step 1: Create ProjectStart by creating an empty AndroidStudio project. Step 2: DependenciesNo special third party library is needed for this project. Step 3: Design LayoutDesign you MainActivity layout by adding a textview in a ConstraintLayout as shown below: activity_main.xml Step 4: Write CodeStart by adding imports: Create MainActivity by extending AppCompatActivity : Define a download ID: Override onCreate() callback: Create an annonymouse BroadcastReceiver and inside it override the onReceive() as follows: Create a Download Uri: Initialize a DownloadRequest: Enqueue the Download: Register the BroadcastReceiver: Here is the full code: MainActivity.kt Step 5: Add permissionsIn your AndroidManifest add the following permissions: Copy the code or download it in the link below, build and run. ReferenceHere are the reference links:
Example 3: Kotlin Android – Dynamic DownloadIn this example you learn how to download from any link. The link is provided at runtime via an EditText. Step 1: Create ProjectStart by creating an empty AndroidStudio project. Step 2: DependenciesNo third party dependency is needed for this project. Step 3: Design LayoutAdd an EditText and Button in your mainactivity’s layout: activity_main.xml Step 4: Write CodeStart by adding imports to your MainActivity.kt as follows: Create the Activity: Define instance fields: Create an annonymouse BroadcastReceiver class and implement the onReceive() method as follows: Setup event handlers: Here is the function that downloads the item: Unregister the BroadcastReceiver in the onCreate() : Here is the full code: MainActivity.kt Android ManifestIn the AndroidManifest.xml add the following permissions: Copy the code or download it in the link below, build and run. ReferenceHere are the reference links:
Example 4: Kotlin DownloadManager with Runtime Permissions and CoroutinesHere is yet another DownloadManager example written in Kotlin. This utilizes Kotlin Coroutines. It also involves checking for necessary permissions at runtime before the download. Step 1: Create ProjectStart by creating an empty AndroidStudio project. Step 2: DependenciesNo third party dependency is used. Step 3: Design LayoutDesign a simple layout with a button and a textview: activity_main.xml Step 4: Write CodeStart by adding imports: Extend the AppCompatActivity :` Then define a download URL as an instance field: Override the onCreate() : When the download button is clicked, before initiating download we check for permission: If granted we initiate the download. Here is the function to initiate download the image: Here is the function that constructs and returns the Download status as a string that can be displayed to the user: The following function will allow you to ask for the necessary permissions before we proceed with the download: Then handle the permission request resulytusing the following callback: Here is the full code: MainActivity.kt Copy the code or download it in the link below, build and run. ReferenceHere are the reference links:
OclemyThanks for stopping by. My name is Oclemy(Clement Ochieng) and we have selected you as a recipient of a GIFT you may like ! Together with Skillshare we are offering you PROJECTS and 1000s of PREMIUM COURSES at Skillshare for FREE for 1 MONTH. To be eligible all you need is by sign up right now using my profile . Источник |