1

I look into some threads to find out how to save photo into SD card. All of them said use "Environment.getExternalStorageDirectory()" to get SD card path. I have tried it, but I got "/storage/emulated/0/" which is not SD card by phone. Can anyone help me?

My device is Samsung Galaxy J7 and A5.

My another part of code using DowloadManager to save photo from url. I want to know also how to set up it to save into SD card.

    DownloadManager mgr = (DownloadManager) mPhotosViewActivity.getSystemService(Context.DOWNLOAD_SERVICE);
    String uRl = imageUrl;
    Uri downloadUri = Uri.parse(uRl);
    DownloadManager.Request request = new DownloadManager.Request(
        downloadUri);

    request.setAllowedNetworkTypes(
        DownloadManager.Request.NETWORK_WIFI
                | DownloadManager.Request.NETWORK_MOBILE)
        .setAllowedOverRoaming(false).setTitle("MyFolder")
        .setDescription("Photo downloaded.")
        .setDestinationInExternalPublicDir("/MyFolder", "test2.jpg");

Finally I found that I am using Lollipop and I have to use "Storage Access Framework" as this link

Community
  • 1
  • 1

2 Answers2

1

On Android 4.4+, you can use getExternalFilesDirs(), getExternalCacheDirs(), and getExternalMediaDirs() on Context (note: all are plural). If they return 2+ entries, the second and subsequent ones will be on removable storage. These directories you can read from and write to without any permissions.

Or, on Android 4.4+, you can use the Storage Access Framework, to ask the user where to store the photo. The user may have the option of choosing a location on removable storage.

Otherwise, on Android 4.4+, you do not have direct access to the filesystem of removable storage.

Environment.getExternalStorageDirectory() is for external storage. Usually, that is not on removable media.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
1
String MY_FOLDER = "MyFolder";
// this method will give you the path of external storage
// which may be your internal storage directory.
String extStore = System.getenv("EXTERNAL_STORAGE");
File f_exts = new File(extStore, MY_FOLDER);

//this method will give you the secondary storage path
//which is memory SD Card path
String secStore = System.getenv("SECONDARY_STORAGE");
File f_secs = new File(secStore, MY_FOLDER);

Refer this answer for more details.

Hope it'll work.

Community
  • 1
  • 1
ELITE
  • 5,815
  • 3
  • 19
  • 29