0

In KOTLIN, all alternatives have been tested but a bitmap image could not be saved to an External Storage

If I use:

MediaStore.Images.Media.insertImage(this.contentResolver, file.absolutePath, file.name, file.name)

Then, I got "insertImage is deprecated in Java"

If I use:

values.put(MediaStore.Images.Media.DATA, file.absolutePath) context.contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)

Then, I got " DATA is deprecated in Java "

Also

Environment.getExternalStorageDirectory() is deprecated

How to solve this problem?

Member
  • 87
  • 2
  • 9

2 Answers2

0

Use insert() to obtain an uri. Open an outputstream for the uri and compress your bitmap to it. Android Q?

blackapps
  • 8,011
  • 2
  • 11
  • 25
  • It is in Android Studio 3.5.3 I used these codes given by Bao Lei at https://stackoverflow.com/questions/36624756/how-to-save-bitmap-to-android-gallery ( begins with private fun saveImage () ). @blackapps, thanks. Could you pls write codes, I did not understand your suggestion – Member Feb 23 '20 at 11:26
  • To avoid getExternalStorageDirectory (since it is deprecated), I put val directory = File(this.applicationContext.getExternalFilesDir(null)?.absolutePath + folderName). BUT, what should be written to solve "DATA is deprecated" problem ? – Member Feb 23 '20 at 11:38
  • Have a look at posts with tag `mediastore`.Add such a tag to your post too please. – blackapps Feb 23 '20 at 11:39
  • Here is an example: https://stackoverflow.com/questions/59991109/download-large-file-with-mediastore-android-10 – blackapps Feb 23 '20 at 11:43
  • Thanks, but, unlike the example, there is no url like " https://httpbin.org/image" to get. Users are cropping images and then they will save to external storages. What can be put instead of this url ? – Member Feb 23 '20 at 11:52
  • You dont need that piece of course. You have your bitmap. Now read my answer again. And you still did not tell if you on Android Q. Mentioning Android Studio makes no sense. – blackapps Feb 23 '20 at 11:53
  • Sorry, but, I am new and dont know how to see whether it is Android Q or not? I can see only its version from the Help tab – Member Feb 23 '20 at 11:57
  • Then what do you see on that tab? – blackapps Feb 23 '20 at 12:00
  • Android Studio 3.5.3 Build #AI-191.8026.42.35.6010548, built on November 15, 2019 JRE: 1.8.0_202-release-1483-b03 amd64 JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o Windows 10 10.0 – Member Feb 23 '20 at 12:20
  • Look in `build.gradle (Module app)` file instead. – blackapps Feb 23 '20 at 12:24
  • And there is a difference for which Android version you develop and which Androifd version runs on your device or emulator. – blackapps Feb 23 '20 at 12:25
  • When I look at the module level build.gradle file, I can see builtscript version for kotlin as ext.kotlin_version = '1.3.41' and dependencies. – Member Feb 23 '20 at 12:40
  • Thank you. The image is downloaded. But, there is a runTime error: Didn't find class "android.provider.MediaStore$Downloads" on path: DexPathList[[zip file "/data/app/com.example.MyApp1-VBKZXuhcU730d_pAAr65Pg==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.MyApp1-VBKZXuhcU730d_pAAr65Pg==/lib/arm64, /data/app/com.example.MyApp1-VBKZXuhcU730d_pAAr65Pg==/base.apk!/lib/arm64-v8a, /system/lib64]] – Member Feb 24 '20 at 05:43
  • The image is downloaded to Phone>Android>Data>com.example.MyApp1>files>MyFiles. But, I could not find solution to this runTime error at this line ==> this.applicationContext.contentResolver.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, contentValues). Why it Didn't find class "android.provider.MediaStore$Downloads" on path: DexPathLis ? – Member Feb 24 '20 at 06:49
  • Please post your complete code. In your post. Not as comment. Also post the stacktrace in your post. You made it all unreadable. – blackapps Feb 24 '20 at 08:33
0

If you want to trully understand what you need you should look here.
But if you worried that Environment.getExternalStorageDirectory() is deprecated you can use Environment.getRootDirectory() or Environment.getRootDirectory()

i30mb1
  • 3,894
  • 3
  • 18
  • 34
  • Thanks, I can get fullPath without using getExternalStorageDirectory(). The problem is insertImage() is deprecated. So, I am trying to download or save with alternative ways. But, I have not saved or download it in KOTLIN. Please help me – Member Feb 23 '20 at 16:27