3

As getExternalStoragePublicDirectory has been deprecated in Android Q and recommendation is to use MediaStore API

Here is my Image URL https://cdn.pixabay.com/photo/2020/04/21/06/41/bulldog-5071407_1280.jpg

I want to download this image file and want to save the shared folder i.e. Downloads or Pictures Folder as per MediaStore API

Below is the sample code

ContentResolver resolver = context.getContentResolver();
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, name);
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, mimeType);
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DOWNLOADS);

Uri uri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues); 

Note: compileSdkVersion 30

I want an example of How to download an Image/Video file in Android R and save it into the download folder location with MediaStore.

Rahul
  • 3,293
  • 2
  • 31
  • 43
Kanaiya Katarmal
  • 5,974
  • 4
  • 30
  • 56
  • `Now question is How to download Image/Video file and save into uri location.` The media store can not help you with downloading so i do not understand the subject of your post. – blackapps Apr 19 '21 at 14:55
  • An uri from the media store or from a file provider. It's all the same. Apparently you wanna download to an uri. Well you can do that exactly the same as before Android R. – blackapps Apr 19 '21 at 14:57
  • @blackapps how to download file? and save into Downloads folder with help of MediaStore – Kanaiya Katarmal Apr 19 '21 at 15:07
  • 1
    As said before: The media store cannot help you with downloading. – blackapps Apr 19 '21 at 15:12
  • @blackapps can you give me one sample for doenload file which support in android R – Kanaiya Katarmal Apr 19 '21 at 15:16
  • https://gitlab.com/commonsguy/cw-android-q/-/tree/vFINAL/ConferenceVideos demonstrates downloading videos using OkHttp and storing them to locations provided by `MediaStore` on Android 10+. – CommonsWare Apr 19 '21 at 15:21
  • "Please share working sample for Android R" -- I did, in my preceding comment. That is described in [this blog post](https://commonsware.com/blog/2019/12/21/scoped-storage-stories-storing-mediastore.html) and [this book](https://commonsware.com/Q). [Here is another sample](https://gitlab.com/commonsguy/cw-android-r/-/tree/vFINAL/VideoTagger), from [this book](https://commonsware.com/R). – CommonsWare May 15 '21 at 15:37

2 Answers2

0

Hi Kanaiya I answered an improvement on MediaStore answer MediaStore.Images.Media.insertImage deprecated

I did use workManager and viewModel not sure if that would apply to your use-case, I did test my answer on API 26 and on API 30.

My working sample is just a 2018 workmanager codelab for which I made minor changes to remove a depreciated api call, thanks for the original idea commonsware

Manav Brar
  • 51
  • 3
0

In order to save it on android 10. Then after calling the insert method you will get the URI then call

os = getContentResolver().openOutputStream(uri);

bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
os.close();

In Android 10 calling insert method is not enough

Your code will look like this

ContentResolver resolver = context.getContentResolver();
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, name);
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, mimeType);
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DOWNLOADS);

Uri uri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues); 

OutputStream os = getContentResolver().openOutputStream(uri);

Bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
os.close();