How to Save Files to Device Storage: Difference between revisions

From wizarPOS
No edit summary
(Replaced content with "{{Migrating|https://smartpossdk.gitbook.io/cloudpossdk/faq/other-development/save-files-locally}}")
Tag: Replaced
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
== Android Storage Permissions Explanation ==
{{Migrating|https://smartpossdk.gitbook.io/cloudpossdk/faq/other-development/save-files-locally}}
'''For Android SDK below 30, directly request the following permissions in AndroidManifest.xml:'''
 
<source lang="xml">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</source>
 
'''For Android SDK above 30, the following permissions need to be requested:'''
 
<source lang="xml">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>(New in Android 11 and above)
</source>
 
If only the following read and write storage permissions are requested on Android 11 and above:
<tt>android.permission.READ_EXTERNAL_STORAGE</tt> and <tt>android.permission.WRITE_EXTERNAL_STORAGE</tt>, it only allows access to public storage, such as Downloads/, Documents/, etc., and does not allow operations on the terminal's file management. If you need access to all files in the shared storage space, you need to request <tt>android.permission.MANAGE_EXTERNAL_STORAGE</tt>.
 
=== Save to a Public Directory ===
If you want to save public files on the external storage, use the <tt>getExternalStoragePublicDirectory()</tt> method to get a File representing the appropriate directory on the external storage. The method takes an argument specifying the type of file you want to save so that they can be logically organized with other public files, such as DIRECTORY_MUSIC or DIRECTORY_PICTURES. For example:
<source lang="java">
public File getPublicAlbumStorageDir(String albumName) {
    // Get the directory for the user's public pictures directory.
    File file = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES), albumName);
    if (!file.mkdirs()) {
        Log.e(LOG_TAG, "Directory not created");
    }
    return file;
}
</source>
 
=== Save to a Private Directory ===
If you want to save files on external storage that are private to your app and not accessible by the MediaStore content provider, you can acquire a directory that's used by only your app by calling <tt>getExternalFilesDir()</tt> and passing it a name indicating the type of directory you'd like. Each directory created this way is added to a parent directory that encapsulates all your app's external storage files, which the system deletes when the user uninstalls your app.
 
''Caution:'' Files on external storage are not always accessible, because users can mount the external storage to a computer for use as a storage device. So if you need to store files that are critical to your app's functionality, you should instead store them on internal storage.
 
For example, here's a method you can use to create a directory for an individual photo album:
<source lang="java">
public File getPrivateAlbumStorageDir(Context context, String albumName) {
    // Get the directory for the app's private pictures directory.
    File file = new File(context.getExternalFilesDir(
            Environment.DIRECTORY_PICTURES), albumName);
    if (!file.mkdirs()) {
        Log.e(LOG_TAG, "Directory not created");
    }
    return file;
}
</source>
 
If none of the pre-defined sub-directory names suit your files, you can instead call <tt>getExternalFilesDir()</tt> and pass null. This returns the root directory for your app's private directory on the external storage.
 
Remember that <tt>getExternalFilesDir()</tt> creates a directory that is deleted when the user uninstalls your app. If the files you're saving should remain available after the user uninstalls your app—such as when your app captures photos and the user should keep those photos—you should instead save the files to a public directory.
 
Regardless of whether you use <tt>getExternalStoragePublicDirectory()</tt> for files that are shared or <tt>getExternalFilesDir()</tt> for files that are private to your app, it's important that you use directory names provided by API constants like DIRECTORY_PICTURES. These directory names ensure that the files are treated properly by the system. For instance, files saved in DIRECTORY_RINGTONES are categorized by the system media scanner as ringtones instead of music.
 
=== Select Between Multiple Storage Locations ===
Sometimes, a device that allocates a partition of the internal memory for use as the external storage also provides an SD card slot. This means that the device has two different external storage directories, so you need to select which one to use when writing "private" files to the external storage.
 
Beginning with Android 4.4 (API level 19), you can access both locations by calling <tt>getExternalFilesDirs()</tt>, which returns a File array with entries for each storage location. The first entry in the array is considered the primary external storage, and you should use that location unless it's full or unavailable.
 
If your app supports Android 4.3 and lower, you should use the support library's static method, <tt>ContextCompat.getExternalFilesDirs()</tt>. This always returns a File array, but if the device is running Android 4.3 and lower, then it contains just one entry for the primary external storage (if there's a second storage location, you cannot access it on Android 4.3 and lower).
 
'''<tt>getExternalFilesDirs()</tt>:'''
This method takes a String argument, type, which specifies the type of files directory to return. For example, <tt>Environment.DIRECTORY_PICTURES</tt> can be used if you are interested in the directory for storing pictures. If type is null, it returns the root of the files directory.
 
The method returns an array of File objects, one for each external storage device. The first entry in the array is the same as the file returned by <tt>getExternalFilesDir(String)</tt>, which is the primary external storage. Additional entries in the array represent any additional mounted external storage devices, such as SD cards or USB drives.
 
Access to the external storage requires appropriate permissions. Before API level 19 (Android 4.4 KitKat), read and write permissions are needed. Starting from API level 19, only read permission is required to read from external storage, and no permissions are required to write to external storage in the directories returned by this method.
 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
 
<source lang="java">
public class MyActivity extends AppCompatActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
 
        // Get all available external file directories (including SD card paths)
        File[] externalFilesDirs = getExternalFilesDirs(null);
 
        for (File file : externalFilesDirs) {
            // Process each path as needed
            Log.d("ExternalFilesDir", "Path: " + file.getAbsolutePath());
        }
    }
}
</source>
 
PS.<tt>getExternalStoragePublicDirectory()</tt>/ <tt>getExternalFilesDir()</tt>, the return of these method is the primary external storage(/storage/emulated/0/).
 
''See Also:'' [https://developer.android.com/training/data-storage/files#WriteExternalStorage Data and file storage overview] from developer.android.com.

Latest revision as of 03:26, 8 April 2024

Please visit new link of same subject:

https://smartpossdk.gitbook.io/cloudpossdk/faq/other-development/save-files-locally

We're making a move! Our site's content is migrating to a new URL, to provide you with an enhanced browsing experience. Please update your bookmarks accordingly. Thank you for your continuous support!