Question

I am making one android app, for which I want to store the downloaded images to sdcard or external storage. The problem I am facing is that on some phone the images are being stored on sdcard whereas in some phones it is being stored in internal memory, specifically in samsung devices like Galaxy S4. Please see the attached images for the file path in S4.

File wallpaperDirectory = new File(
                    Environment.getExternalStorageDirectory()
                            + "/TestApp/");
            wallpaperDirectory.mkdirs();
            FileOutputStream out = new FileOutputStream(Environment
                    .getExternalStorageDirectory().toString()
                    + "/TestApp/" + secret + ".jpg");
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
            Toast.makeText(
                    getApplicationContext(),
                    "Wallpaper saved sucessfully \n"
                            + Environment.getExternalStorageDirectory()
                                    .toString() + "/TestApp/" + secret
                            + ".jpg", Toast.LENGTH_SHORT).show();
            sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
                    Uri.parse("file://"
                            + Environment.getExternalStorageDirectory())));

storage on s4

Was it helpful?

Solution

I am making one android app, for which I want to store the downloaded images to sdcard or external storage.

The Android SDK offers access to external storage. Whether this is "sdcard" or something else is up to the device manufacturer.

The problem I am facing is that on some phone the images are being stored on sdcard whereas in some phones it is being stored in internal memory, specifically in samsung devices like Galaxy S4.

This code stores the images on external storage. It is not storing them on internal storage. Whether external storage is removable or not is up to the device manufacturer.

You may wish to consider reading the documentation about external storage.

You should also:

  • Not use string concatenation to create File objects. Use the proper File constructor.

  • Store your data somewhere better than in a hardcoded TestApp directory off the root of external storage, such as getExternalFilesDir() or getExternalCacheDir(). Your approach has been considered poor form for years, as it clutters up the user's external storage.

  • Use a more-focused approach for updating MediaStore, such as using MediaScannerConnection and scanFile(), rather than sending out an operating system broadcast, as if you were the operating system.

  • Be sure to do all of this work on a background thread (it is unclear whether you are or are not from the code snipppet).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top