[英]Hide a file from SD card
嗨,我在SD卡中創建了一個文件夾,其中包含一個db
public Databasehandler(Context context)
{
//super(context, DATABASE_NAME, null, DATABASE_VERSION);
super(context, Environment.getExternalStorageDirectory()+ File.separator + "MyAppFolder"+ "/"+DATABASE_NAME, null, DATABASE_VERSION);
}
現在,我正在嘗試隱藏此文件,以使其無法訪問。 我嘗試了以下代碼。
final String NOMEDIA_FILE = ".nomedia";
path = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+ File.separator + "MyAppFolder" );
path.mkdirs();
file= new File(path,NOMEDIA_FILE);
if (!file.exists())
{
try
{
file.createNewFile();
Log.e("NOMEDIA_FILE"," ");
}
catch (IOException e)
{
e.printStackTrace();
}
}
但是,這不起作用。 不知道如何在SDcard中隱藏文件。 請幫忙。 謝謝!
如果您使用的是Java 7,則可以使用新的java.nio.file.attribute包,如下所示:
Path path = FileSystems.getDefault().getPath("/j", "sa");
Files.setAttribute(path, "dos:hidden", true);
或者,如果您使用的是Java的舊版本,並且/或者想使用運行時,請嘗試以下操作:
Process process = Runtime.getRuntime().exec("cmd.exe /C attrib -s -h -r your_path");
你可以這樣使用
final String NOMEDIA_FILE = ".nomedia";
path = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+ File.separator + "MyAppFolder" );
path.mkdirs();
file= new File(path,NOMEDIA_FILE);
Files.setAttribute(path, "dos:hidden", true);
要么
Process process = Runtime.getRuntime().exec("cmd.exe /C attrib -s -h -r"+path);
if (!file.exists())
{
try
{
file.createNewFile();
Log.e("NOMEDIA_FILE"," ");
}
catch (IOException e)
{
e.printStackTrace();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.