简体   繁体   中英

Android storing images in a hidden folder

I'm downloading images from a server and storing them in the internal storage of my device in a folder. This is the code :

//Here I create the folder
File folder = new File(Environment.getExternalStorageDirectory() + "/myfolder");
boolean success = true;
if (!folder.exists()) {
success = folder.mkdir();
}
startDownload();

The function startDownload() connects to a php page and download images from the server to the precised directory. When entering the file manager in my device the folder is shown along with its content. Is there anyway that I can download the images to a hidden directory so the user won't see the images ?

This is elementary Linux knowledge. Prepending a file with ".", such that—for example—the file equals .myFolder , will hide it by default from file managers. However most every file manager also has the option to show hidden files, so take that into account.

I suggest, if you really never want the user to see the files, that you use the private internal storage. We can accomplish this with:

String FILENAME = "hello_file";
String string = "hello world!";

FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
//fos.write(string.getBytes()); writes string to file
fos.close();

I would imagine the download portion of your program makes use of an OutputStream of sorts. Use the FileOutputStream we defined in the snippet above, instead of the OutputStream you've got now, and you'll be writing to the private internal storage!

This also has the added benefit of clearing said files upon user uninstall, whereas, writing to external SD will leave fragments of our app on the sdcard even after uninstall, unless explicitly deleted .

Also, please visit the official documentation here .

 Is there anyway that I can download the images to a hidden directory so the user won't see the images ? 

Yes.you can store images into data directory of your app.

String dir=Environment.getDataDirectory()+"/myfolder";

or you can simple make a hidden folder by preceding folder name with dot.see below :

String dir=Environment.getExternalStorageDirectory() + "/.myfolder"

NOTE : Folder name prefix by dot(.) will be visible if user sets "Show Hidden Files" from File manager.

So better use data directory.

Hope it will help.Thanks

I think external cache directory is designed for this purpose but remember to monitor it and delete stuff you no longer need:

http://developer.android.com/reference/android/content/Context.html#getExternalCacheDirs()

from the documentation:

Returns absolute paths to application-specific directories on all external storage devices where the application can place cache files it owns. These files are internal to the application, and not typically visible to the user as media.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM