简体   繁体   English

Android将图像存储在隐藏的文件夹中

[英]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. 函数startDownload()连接到php页面,并将图像从服务器下载到精确的目录。 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. 这是基本的Linux知识。 Prepending a file with ".", such that—for example—the file equals .myFolder , will hide it by default from file managers. 在文件前添加“。”,例如,该文件等于.myFolder默认情况下将从文件管理器隐藏该文件。 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. 我可以想象您的程序的下载部分将利用各种OutputStream 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! 使用我们在上面的代码段中定义的FileOutputStream ,而不是您现在拥有的OutputStream ,您将写入私有内部存储!

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 . 这还具有在用户卸载时清除所述文件的额外好处,而写入外部SD甚至在卸载后仍会将我们应用程序的片段保留在sdcard上, 除非明确删除

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 : 或者,您也可以在文件夹名称前加dot来简单地创建一个隐藏文件夹。请参见下文:

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() 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. 这些文件是应用程序的内部文件,通常作为介质对用户不可见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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