Android: Cannot save file to external SD Card even though I have the correct Path

孤者浪人 提交于 2019-12-25 00:47:55

问题


so basically my problem is quite simple and yet I haven't found any solution anywhere for this.

First of all, I've been testing this code on the:

  • Samsung Galaxy Tab Note 10.1 [2014] SM-P605 running Android 5.1.1, API 22 with a 32GB external SD Card installed on it.
  • Google Pixel 3 XL running Android 9, API 28

Long story short, I'm making an application and at some stage this app needs to save an image file (a JPG image file) either in:

  • primary shared storage "Pictures" directory /storage/emulated/0/Pictures or
  • inside the external SD Card if the device can support one AND if it has one installed, i.e., inside something like /storage/extSdCard/.

It is completely up to the user of the app where they want to save the image.

Now, saving into the primary (public) shared storage, /storage/emulated/0/Pictures is really easy, no problem there at all (literally no problem at all on both the Pixel 3 XL and the Galaxy Note 10.1, it's all good). My problem is with saving it into the external SD Card.

I used this code below to get the path to the external SD Card:

    String sdCardPath = System.getenv("SECONDARY_STORAGE");

If I try to print out sdCardPath above, I will get /storage/extSdCard/ which is completely right, I checked the external SD Card Path on my Galaxy Note with ES File Explorer and it's the same with the sdCardPath.

However, when I'm saving my JPG file into sdCardPath, even though it looks like the file is saved there without any problem (my log didn't output any error), for some reason the image isn't saved there, there's literally nothing new saved into /storage/extSdCard.

I've also tried to create the Pictures folder inside the external SD Card but whenever I executed this block of code:

    //check if the folder already exists
    if (!filePath.exists()) {
        if (!filePath.mkdirs()) {
            showToast("Problem creating the picture directory");
        } else {
            showToast("Successfully created the folder \"Pictures\"");
        }
    }

It always shows the toast that says Problem creating the picture directory

Again, this only happens if I try to save the picture into the external SD Card. There's no problem with saving it into the /storage/emulated/0/Pictures.

I've also used these permissions inside the AndroidManifest.xml already:

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

So yeah, if anyone has any knowledge about this, would you be so kind as to share it with me or to give me a little clue as to what's going on? I just want to be able to save the pictures into the external SD Card and if possible, create the Pictures folder in the SD Card to save all the pictures there.

Kind regards

来源:https://stackoverflow.com/questions/55863907/android-cannot-save-file-to-external-sd-card-even-though-i-have-the-correct-pat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!