我正在制作一个Android应用程序,并且我想阻止用户打开某个文件夹。在该文件夹中,用户可以存储图像或视频文件。如果我可以使用密码保护该文件夹,那将是一件好事。如何以编程方式锁定Android文件夹?
这是最好的方法吗?
我正在制作一个Android应用程序,并且我想阻止用户打开某个文件夹。在该文件夹中,用户可以存储图像或视频文件。如果我可以使用密码保护该文件夹,那将是一件好事。如何以编程方式锁定Android文件夹?
这是最好的方法吗?
您应该将此信息保存在内部存储器上。通常其他应用程序无法访问这些文件。从限制:
您可以直接在设备的内部存储上保存文件。默认情况下,保存到内部存储的文件对于您的应用程序是私人的,其他应用程序无法访问它们(用户也无法访问)。当用户卸载您的应用程序时,这些文件将被删除。
见链接:http://developer.android.com/guide/topics/data/data-storage.html#filesInternal
因此,当我从这个链接中获得时,我只需要将应用程序文件保存到内部存储上的某个文件夹中,那将是私人的? – Zookey 2013-03-22 11:47:19
是的,但不要忘记私人模式:FileOutputStream fos = openFileOutput(FILENAME,Context.MODE_PRIVATE); 另请注意,有根设备可能能够访问这些文件。 – RobinDeCroon 2013-03-22 11:50:22
insted的LOCK的,我会saggest你让文件夹.
像文件夹名 - >.test
用户倾斜这里看到该文件夹是代码
File direct = new File(Environment.getExternalStorageDirectory() + "/.test");
if(!direct.exists())
{
if(direct.mkdir())
{
//directory is created;
}
}
上面的代码是创建名称为“.test”的文件夹(SD卡中),然后保存你的数据(文件,视频..无论)在这个文件夹中的用户不能访问它。
如果您在内部存储器中创建文件夹,然后如果用户清除您的应用程序的数据,那么该文件夹可能会EMPTY
!
这是在Sdcard文件夹中加密和解密文件的功能。 我们无法锁定文件夹,但我们可以在Android中使用AES加密文件,它可以帮助您。
static void encrypt() throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
// Here you read the cleartext.
FileInputStream fis = new FileInputStream("data/cleartext");
// This stream write the encrypted text. This stream will be wrapped by another stream.
FileOutputStream fos = new FileOutputStream("data/encrypted");
// Length is 16 byte
SecretKeySpec sks = new SecretKeySpec("MyDifficultPassw".getBytes(), "AES");
// Create cipher
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, sks);
// Wrap the output stream
CipherOutputStream cos = new CipherOutputStream(fos, cipher);
// Write bytes
int b;
byte[] d = new byte[8];
while((b = fis.read(d)) != -1) {
cos.write(d, 0, b);
}
// Flush and close streams.
cos.flush();
cos.close();
fis.close();
}
static void decrypt() throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
FileInputStream fis = new FileInputStream("data/encrypted");
FileOutputStream fos = new FileOutputStream("data/decrypted");
SecretKeySpec sks = new SecretKeySpec("MyDifficultPassw".getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, sks);
CipherInputStream cis = new CipherInputStream(fis, cipher);
int b;
byte[] d = new byte[8];
while((b = cis.read(d)) != -1) {
fos.write(d, 0, b);
}
fos.flush();
fos.close();
cis.close();
}
我不知道最好的方式..但最简单的(或最便宜)的方法是使用应用的私有文件夹(仅适用于您的应用程序可以访问),并在其顶部提供一个UI .. – Madushan 2013-03-22 11:43:34
凉。但我怎样才能使私人文件夹?有没有例子? – Zookey 2013-03-22 11:45:15
请参阅http://developer.android.com/guide/topics/data/data-storage.html#filesInternal每个应用程序都只能获取该应用程序可以访问的私人文件夹。 (Android管理安全性,它在内部存储上,因此无法通过USB访问)您可以使用上述API来操作它。 – Madushan 2013-03-22 11:47:27