Android environment directory downloads

Содержание
  1. lopspower / README.md
  2. Environment
  3. Class Overview
  4. Summary
  5. Constants
  6. public static final String MEDIA_BAD_REMOVAL
  7. public static final String MEDIA_CHECKING
  8. public static final String MEDIA_MOUNTED
  9. public static final String MEDIA_MOUNTED_READ_ONLY
  10. public static final String MEDIA_NOFS
  11. public static final String MEDIA_REMOVED
  12. public static final String MEDIA_SHARED
  13. public static final String MEDIA_UNMOUNTABLE
  14. public static final String MEDIA_UNMOUNTED
  15. Fields
  16. public static String DIRECTORY_ALARMS
  17. public static String DIRECTORY_DCIM
  18. public static String DIRECTORY_DOWNLOADS
  19. public static String DIRECTORY_MOVIES
  20. public static String DIRECTORY_MUSIC
  21. public static String DIRECTORY_NOTIFICATIONS
  22. public static String DIRECTORY_PICTURES
  23. public static String DIRECTORY_PODCASTS
  24. public static String DIRECTORY_RINGTONES
  25. Public Constructors
  26. public Environment ()
  27. Public Methods
  28. public static File getDataDirectory ()
  29. public static File getDownloadCacheDirectory ()
  30. public static File getExternalStorageDirectory ()
  31. public static File getExternalStoragePublicDirectory (String type)
  32. Kotlin Android DownloadManager Tutorial and Examples
  33. What is DownloadManager?
  34. Why Download?
  35. Common Questions
  36. Which Types of Downloads are Handled by DownloadManager
  37. Where Do the Downloads take Place?
  38. Why Use the DownloadManager?
  39. DownloadManager Quick HowTos and Snippets
  40. DownloadManager Examples
  41. Example 1. Download File, View All Download, Delete Download
  42. Video Tutorial
  43. OverView of the App
  44. Example 2: Kotlin Android simple DownloadManager Example
  45. Step 1: Create Project
  46. Step 2: Dependencies
  47. Step 3: Design Layout
  48. Step 4: Write Code
  49. Step 5: Add permissions
  50. Reference
  51. Example 3: Kotlin Android – Dynamic Download
  52. Step 1: Create Project
  53. Step 2: Dependencies
  54. Step 3: Design Layout
  55. Step 4: Write Code
  56. Android Manifest
  57. Reference
  58. Example 4: Kotlin DownloadManager with Runtime Permissions and Coroutines
  59. Step 1: Create Project
  60. Step 2: Dependencies
  61. Step 3: Design Layout
  62. Step 4: Write Code
  63. Reference
  64. 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

Constants

public static final String MEDIA_BAD_REMOVAL

getExternalStorageState() returns MEDIA_BAD_REMOVAL if the media was removed before it was unmounted.

public static final String MEDIA_CHECKING

getExternalStorageState() returns MEDIA_CHECKING if the media is present and being disk-checked

public static final String MEDIA_MOUNTED

getExternalStorageState() 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_ONLY

getExternalStorageState() 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_NOFS

getExternalStorageState() returns MEDIA_NOFS if the media is present but is blank or is using an unsupported filesystem

public static final String MEDIA_REMOVED

getExternalStorageState() returns MEDIA_REMOVED if the media is not present.

public static final String MEDIA_SHARED

getExternalStorageState() returns MEDIA_SHARED if the media is present not mounted, and shared via USB mass storage.

public static final String MEDIA_UNMOUNTABLE

getExternalStorageState() 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_UNMOUNTED

getExternalStorageState() returns MEDIA_UNMOUNTED if the media is present but not mounted.

Fields

public static String 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). 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_DCIM

The 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_DOWNLOADS

Standard 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_MOVIES

Standard 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_MUSIC

Standard 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_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). 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_PICTURES

Standard 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_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). 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_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). 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 Constructors

public Environment ()

Public Methods

public 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 Also

public 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 Examples

Android 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 Questions

Here are some of the questions to allow us understand the DownloadManager class.

Which Types of Downloads are Handled by DownloadManager

Normally 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

  1. As we said the downloads take place in the background thread. In fact in a background in a system app. This means our app doesn’t have to handle the downloads manually in our main thread hence our UI is always responsive. This fact is quite important. Downloading data is one of the most time and resource consuming tasks personal gadgets do. Yet it is one of those tasks that really make devices as powerful as they are. So we have to do them. But we have to do them in a background thread and DownloadManager definitely obeys that.
  2. Secondly the HTTP interactions are abstracted away from us. We don’t have to worry about variou HTTP codes and messages and possible failures. In fact even retries are made on our behalf. Yet these retries can amazingly be persisted through connectivity state changes and system reboots. Progress can be shown to us as the download takes place hence making it very user friendly.

DownloadManager Quick HowTos and Snippets

1. How to download videos using DownloadManager class

We 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 Examples

Let’s look at some DownloadManager examples.

Example 1. Download File, View All Download, Delete Download

In 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 Tutorial

Here we have a video tutorial for this example.

Here’s the demo of the app.

OverView of the App

  1. User clicks the download button.
  2. Download starts.
  3. Meanwhile the progress is shown in the status bar as a notification. The notification also includes the title and description of the download.
  4. The user is notified via the system status bar text when the download is complete.
  5. When user clicks the notification the file is opened. If it is not found then a toast message is shown.
  6. When user clicks View All button then the view for displaying all the downloads is shown. You may have some other downloads enqueued there, some not from your application.
  7. When user clicks the Delete button then our download whether partial or complete gets deleted.
(a). MainActivity.java

MainActivity 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:

  1. DownloadManager – The class responsible for allowing us use the system download manahger.
  2. Intent – Class responsible for allowing us open the system download manager view.
  3. Uri – Allows us to parse a string url into a Uri from which our file can be downloaded.
  4. Environment – Allow us specify the public directory where our downloaded file will be stored.
  5. Toast – Allow us to show quick messages.

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

We need a layout for our MainActivity . Here’s the code.

(c). AndroidManifest.xml

In 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:

Download

Here are reference resources:

No. Location Link
1. GitHub Direct Download
2. GitHub Browse
3. YouTube Video Tutorial
4. YouTube ProgrammingWizards TV Channel

Example 2: Kotlin Android simple DownloadManager Example

A simple open source Kotlin downloadmanager example suitable for absolute beginners.

Step 1: Create Project

Start by creating an empty AndroidStudio project.

Step 2: Dependencies

No special third party library is needed for this project.

Step 3: Design Layout

Design you MainActivity layout by adding a textview in a ConstraintLayout as shown below:

activity_main.xml

Step 4: Write Code

Start 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 permissions

In your AndroidManifest add the following permissions:

Copy the code or download it in the link below, build and run.

Reference

Here are the reference links:

Number Link
1. Download Example
2. Follow code author

Example 3: Kotlin Android – Dynamic Download

In this example you learn how to download from any link. The link is provided at runtime via an EditText.

Step 1: Create Project

Start by creating an empty AndroidStudio project.

Step 2: Dependencies

No third party dependency is needed for this project.

Step 3: Design Layout

Add an EditText and Button in your mainactivity’s layout:

activity_main.xml

Step 4: Write Code

Start 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 Manifest

In the AndroidManifest.xml add the following permissions:

Copy the code or download it in the link below, build and run.

Reference

Here are the reference links:

Number Link
1. Download Example
2. Follow code author

Example 4: Kotlin DownloadManager with Runtime Permissions and Coroutines

Here 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 Project

Start by creating an empty AndroidStudio project.

Step 2: Dependencies

No third party dependency is used.

Step 3: Design Layout

Design a simple layout with a button and a textview:

activity_main.xml

Step 4: Write Code

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

Reference

Here are the reference links:

Number Link
1. Download Example
2. Follow code author

report this ad

Oclemy

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

Источник

Читайте также:  Самсунг а50 выдает android recovery что делать
Оцените статью