2

With the recent release of Android Q Beta, getExternalStoragePublicDirectory() became deprecated. Now if someone wants to create a folder he has to either use getExternalFilesDir() or use MediaStore. But I am developing an app where the user clicks to download his profile picture and I download it using Firebase Storage. Now, the download process works flawlessly, the real problem is storing it in a file. Now I don't want to store in my private data folder like:

File file = new File(getExternalFilesDir(), "file.jpg");

I want it to download in the Downloads folder, so I need to use the deprecated method. Is there a new way to store files in a folder like the Downloads, or Music for Android Q and later? What should I do?

Note: I had asked this question yesterday in a different manner, but the answers did not satisfy me. So I decided to ask this question with more specific details.


Edit: I did check the documentation here: https://developer.android.com/preview/privacy/scoped-storage. In fact, I've been researching for the previous three days, but I don't know how to implement it in my app. An example would be appreciated.

Also, I'm using Java, and not Kotlin.

4
  • Why are you not using getExternallFilesDir() or MediaStore?
    – Yash
    Aug 26, 2019 at 10:52
  • 1
    @YashKrishan I don't. I want to store the file in the Downloads Folder. But in Android Q the only folders I can access is getExternalFilesDir() or the data directory of my app. Aug 26, 2019 at 10:54
  • 1
    the answer is here: developer.android.com/guide/topics/providers/document-provider . please read carefully, I found it
    – Zun
    Aug 26, 2019 at 11:05
  • Yes, it looks like these changes in Android Q are really bad for developers, please check also here @CommonsWare's post: commonsware.com/blog/2019/06/07/…
    – MrVasilev
    Aug 26, 2019 at 11:08

2 Answers 2

2
  1. First,download the image and save to your private folder like so: File file = new File(getExternalFilesDir(), "file.jpg");

  2. Next, grab the content:// uri of the just downloaded image file like so: Uri sourceUri = resolver.insert( imageCollections, values);

  3. Then, grab the content:// uri of the destination file in the Download folder of external storage like so: Uri destinationUri = data.getData();

  4. Create InputStream from the sourceUri like so: FileInputStream fis = ( FileInputStream ) this.getContentResolver().openInputStream( sourceUri);

  5. Create OutputStream from the destinationUri like so: FileOutputStream fos = ( FileOutputStream ) this.getContentResolver().openOutputStream( destination );

  6. copy file from source to destination .


All together: //after image file is successfully downloaded...

String path = fullPathToRecentlyDownloadedImage;

path should be like:/storage/emulated/0/Android/data/yourAppPackageName/files/ImageFolder/imageName.png

Dont hard code it.

Uri imageCollections = MediaStore.Image.Media.getContentUri( "external" );

 ContentValues values = new ContentValues();
    values.put( MediaStore.Image.Media.DISPLAY_NAME, nameOfImage );
    values.put(  MediaStore.Image.Media.MIME_TYPE, imageMimeType );
    values.put(  MediaStore.Image.Media.DATE_ADDED, dateAdded );
    values.put(  MediaStore.Image.Media.DATA, path );
    Uri sourceUri= resolver.insert( imageCollections , values);

Then, grab the content:// uri of the destination file in the Download folder of external storage

   Intent i = new Intent();
   if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT ) {
        i.setAction( Intent.ACTION_CREATE_DOCUMENT );
        i.addCategory( Intent.CATEGORY_OPENABLE );
        i.setType( imageMimeType );
        i.putExtra( Intent.EXTRA_TITLE, nameOfImage );
        startActivityForResult( i, 1000 );
    }

The content:// uri will be pushed to onActivityResult() function of MainActivity.Override this function like so:

@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { 
Super.onActivityResult(requestCode, resultCode, data);
if ( requestCode == 1000 && data != null ){
destinationUri = data.getData();

We now have the destination and source content:// uris, initializing Input and Output streams from these uris as well as copying bytes from source to destination is up to you.

Note:The intent code above will open a save as activity, navigate to the Download folder and click save.

Hope will help

1
  • Thanks will check this out. I am not doing Android Development at the moment, so when I have the time I will check this out and report back. May 10, 2020 at 18:54
0

Did you check this documentation here: https://developer.android.com/preview/privacy/scoped-storage

In order to access any other file that another app has created, including files in a "downloads" directory, your app must use the Storage Access Framework, which allows the user to select a specific file.

Maybe this will help you also: https://developer.android.com/guide/topics/providers/document-provider

5
  • 3
    I did, but I can't understand how to implement it. An example would be great :) Aug 26, 2019 at 10:28
  • @GauravMall Literally, there are examples on that documentation page. It is unclear what you don't understand here. Aug 26, 2019 at 12:46
  • 1
    All examples have Java variant, too. You can switch between Java and Kotlin. It is still not clear how provided examples don't work for you, because it is all there. If there is some particular issue you have with examples, then you should ask about that particular issue. Aug 26, 2019 at 12:53
  • 1
    Examples don't do what I want is rather vague. If something does not work, then provide minimal reproducible example and explain what does not work for you. Without it we can just guess what is your real problem. Aug 26, 2019 at 17:00
  • @GauravMall Not that code, but the example from the documentation page that didn't worked for you. If I copy paste example from there as an answer, you will tell me that it does not work... so to save me and others trouble of doing so... define what does not work and how. Aug 26, 2019 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.