How to directly download a file to Download directory on Android Q (Android 10)

前端 未结 3 1693

Lets say I\'m developing a chat app that is able to share with others ANY kind of files (no mimetype restriction): like images, videos, documents, but also compressed files like

相关标签:
3条回答
  • 2021-02-04 00:24

    I thought that it would best be to write the answer in java, as a lot of legacy code exists out there which would have to be updated. Also I have no idea what kind of file you want to store and so, here, I took an example of a bitmap. If you want to save any other kind of file you just have to create an OutputStream of it, and for the remaining part of it you can follow the code I write below. Now, here, this function is only going to handle the saving for Android 10+ and hence the annotation. I hope you have the prerequisite knowledge on how to save files in lower android versions

    @RequiresApi(api = Build.VERSION_CODES.Q)
    public void saveBitmapToDownloads(Bitmap bitmap) {
        ContentValues contentValues = new ContentValues();
    
        // Enter the name of the file here. Note the extension isn't necessary
        contentValues.put(MediaStore.Downloads.DISPLAY_NAME, "Test.jpg");
        // Here we define the file type. Do check the MIME_TYPE for your file. For jpegs it is "image/jpeg"
        contentValues.put(MediaStore.Downloads.MIME_TYPE, "image/jpeg");
        contentValues.put(MediaStore.Downloads.IS_PENDING, true);
    
        Uri uri = MediaStore.Downloads.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
        Uri itemUri = getContentResolver().insert(uri, contentValues);
    
        if (itemUri != null) {
            try {
                OutputStream outputStream = getContentResolver().openOutputStream(itemUri);
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
                outputStream.close();
                contentValues.put(MediaStore.Images.Media.IS_PENDING, false);
                getContentResolver().update(itemUri, contentValues, null, null);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

    Now if you want to create a sub directory i.e. a folder inside the Downloads folder you could add this to contentValues

    contentValues.put(MediaStore.Downloads.RELATIVE_PATH, "Download/" + "Folder Name");
    

    This will create a folder named "Folder Name" and store "Test.jpg" inside that folder.

    Also do note this doesn't require any permissions in the manifest which implies it doesn't need runtime permissions as well. If you want the Kotlin version do ask me in the comments. I would happily and readily provide the Kotlin method for the same.

    0 讨论(0)
  • 2021-02-04 00:25

    You can use the Android DownloadManager.Request. It will need the WRITE_EXTERNAL_STORAGE Persmission until Android 9. From Android 10/ Q and above it will not need any permission (it seems it handle the permission itself).

    If you want to open the file afterwards, you will need the user's permission instead (also if you only want to open it within an external app (e.g. PDF-Reader).

    You can use the download manager like this:

    DownloadManager.Request request = new DownloadManager.Request(<download uri>);
    request.addRequestHeader("Accept", "application/pdf");
        
    // Save the file in the "Downloads" folder of SDCARD
    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,filename);
    
    DownloadManager downloadManager = (DownloadManager) getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
    downloadManager.enqueue(request);
    

    This are the references: https://developer.android.com/reference/kotlin/android/app/DownloadManager.Request?hl=en#setDestinationInExternalPublicDir(kotlin.String,%20kotlin.String)

    https://developer.android.com/training/data-storage

    0 讨论(0)
  • 2021-02-04 00:26

    More than 10 months have passed and yet not a satisfying answer for me have been made. So I'll answer my own question.

    As @CommonsWare states in a comment, "get MediaStore.Downloads.INTERNAL_CONTENT_URI or MediaStore.Downloads.EXTERNAL_CONTENT_URI and save a file by using Context.getContentResolver.insert()" is supposed to be the solution. I double checked and found out this is true and I was wrong saying it doesn't work. But...

    I found it tricky to use ContentResolver and I was unable to make it work properly. I'll make a separate question with it but I kept investigating and found a somehow satisfying solution.

    MY SOLUTION:

    Basically you have to download to any directory owned by your app and then copy to Downloads folder.

    1. Configure your app:

      • Add provider_paths.xml to xml resource folder

        <paths xmlns:android="http://schemas.android.com/apk/res/android">
            <external-path name="external_files" path="."/>
        </paths>
        
      • In your manifest add a FileProvider:

        <application>
            <provider
                 android:name="androidx.core.content.FileProvider"
                 android:authorities="${applicationId}.provider"
                 android:exported="false"
                 android:grantUriPermissions="true">
                 <meta-data
                     android:name="android.support.FILE_PROVIDER_PATHS"
                     android:resource="@xml/provider_paths" />
             </provider>
         </application>
        
    2. Prepare to download files to any directory your app owns, such as getFilesDir(), getExternalFilesDir(), getCacheDir() or getExternalCacheDir().

      val privateDir = context.getFilesDir()
      
    3. Download file taking its progress into account (DIY):

      val downloadedFile = myFancyMethodToDownloadToAnyDir(url, privateDir, fileName)
      
    4. Once downloaded you can make any threatment to the file if you'd like to.

    5. Copy it to Downloads folder:

      //This will be used only on android P-
      private val DOWNLOAD_DIR = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
      
      val finalUri : Uri? = copyFileToDownloads(context, downloadedFile)
      
      fun copyFileToDownloads(context: Context, downloadedFile: File): Uri? {
          val resolver = context.contentResolver
          return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
              val contentValues = ContentValues().apply {
                  put(MediaStore.MediaColumns.DISPLAY_NAME, getName(downloadedFile))
                  put(MediaStore.MediaColumns.MIME_TYPE, getMimeType(downloadedFile))
                  put(MediaStore.MediaColumns.SIZE, getFileSize(downloadedFile))
              }
              resolver.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, contentValues)
          } else {
              val authority = "${context.packageName}.provider"
              val destinyFile = File(DOWNLOAD_DIR, getName(downloadedFile))
              FileProvider.getUriForFile(context, authority, destinyFile)
          }?.also { downloadedUri ->
              resolver.openOutputStream(downloadedUri).use { outputStream ->
                  val brr = ByteArray(1024)
                  var len: Int
                  val bufferedInputStream = BufferedInputStream(FileInputStream(downloadedFile.absoluteFile))
                  while ((bufferedInputStream.read(brr, 0, brr.size).also { len = it }) != -1) {
                      outputStream?.write(brr, 0, len)
                  }
                  outputStream?.flush()
                  bufferedInputStream.close()
              }
          }
      }
      
    6. Once in download folder you can open file from app like this:

      val authority = "${context.packageName}.provider"
      val intent = Intent(Intent.ACTION_VIEW).apply {
          setDataAndType(finalUri, getMimeTypeForUri(finalUri))
          if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP) {
              addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION)
          } else {
              addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
          }
      }
      try {
          context.startActivity(Intent.createChooser(intent, chooseAppToOpenWith))
      } catch (e: Exception) {
          Toast.makeText(context, "Error opening file", Toast.LENGTH_LONG).show()
      }
      
      //Kitkat or above
      fun getMimeTypeForUri(context: Context, finalUri: Uri) : String =
          DocumentFile.fromSingleUri(context, finalUri)?.type ?: "application/octet-stream"
      
      //Just in case this is for Android 4.3 or below
      fun getMimeTypeForFile(finalFile: File) : String =
          DocumentFile.fromFile(it)?.type ?: "application/octet-stream"
      

    Pros:

    • Downloaded files survives to app uninstallation

    • Also allows you to know its progress while downloading

    • You still can open them from your app once moved, as the file still belongs to your app.

    • write_external_storage permission is not required for Android Q+, just for this purpose:

      <uses-permission
          android:name="android.permission.WRITE_EXTERNAL_STORAGE"
          android:maxSdkVersion="28" />
      

    Cons:

    • You won't have access to downloaded files once after clearing your app data or uninstalling and reinstalling again (they no longer belongs to your app unless you ask for permission)
    • Device must have more free space to be able to copy every file from its original directory to its final destination. This is important speacially for large files. Although if you have access to the original inputStream you could directly write to downloadedUri instead of copying from an intermediary file.

    If this approach is enough for you then give it a try.

    0 讨论(0)
提交回复
热议问题