Skip to content Skip to sidebar Skip to footer

Get Sd Card Path In Android Vs Hard Coded Path

my code works fine, it downloads image to sd card, however , i get this warning where i defined my sd card path 'Do not hardcode '/sdcard/'; use Environment.getExternalStorageDirec

Solution 1:

When working with files and directories, it's better to work with File objects rather than with strings. Here's how you can address the warning:

Filedir= Environment.getExternalStorageDirectory();
FiletmpFile=newFile(dir, ".temp");
OutputStreamoutput=newFileOutputStream(tmpFile);

That creates a File object that points to a file named ".temp" in the environment's external storage directory. It then opens it for writing using a different constructor of the FileOutputStream class.

If you need the file path as a string instead (say, for printing), you can do that too:

StringtmpFileString= tmpFile.getPath();

Or if you decide to use the java.nio API in the future and need a Path object:

PathtmpFilePath= tmpFile.toPath();

Post a Comment for "Get Sd Card Path In Android Vs Hard Coded Path"