2013-03-22 69 views
0

我正在制作一个Android应用程序,并且我想阻止用户打开某个文件夹。在该文件夹中,用户可以存储图像或视频文件。如果我可以使用密码保护该文件夹,那将是一件好事。如何以编程方式锁定Android文件夹?

这是最好的方法吗?

+0

我不知道最好的方式..但最简单的(或最便宜)的方法是使用应用的私有文件夹(仅适用于您的应用程序可以访问),并在其顶部提供一个UI .. – Madushan 2013-03-22 11:43:34

+0

凉。但我怎样才能使私人文件夹?有没有例子? – Zookey 2013-03-22 11:45:15

+0

请参阅http://developer.android.com/guide/topics/data/data-storage.html#filesInternal每个应用程序都只能获取该应用程序可以访问的私人文件夹。 (Android管理安全性,它在内部存储上,因此无法通过USB访问)您可以使用上述API来操作它。 – Madushan 2013-03-22 11:47:27

回答

2

您应该将此信息保存在内部存储器上。通常其他应用程序无法访问这些文件。从限制:

您可以直接在设备的内部存储上保存文件。默认情况下,保存到内部存储的文件对于您的应用程序是私人的,其他应用程序无法访问它们(用户也无法访问)。当用户卸载您的应用程序时,这些文件将被删除。

见链接:http://developer.android.com/guide/topics/data/data-storage.html#filesInternal

+0

因此,当我从这个链接中获得时,我只需要将应用程序文件保存到内部存储上的某个文件夹中,那将是私人的? – Zookey 2013-03-22 11:47:19

+1

是的,但不要忘记私人模式:FileOutputStream fos = openFileOutput(FILENAME,Context.MODE_PRIVATE); 另请注意,有根设备可能能够访问这些文件。 – RobinDeCroon 2013-03-22 11:50:22

2

insted的LOCK的,我会saggest你让文件夹.像文件夹名 - >.test

用户倾斜这里看到该文件夹​​是代码

File direct = new File(Environment.getExternalStorageDirectory() + "/.test"); 

    if(!direct.exists()) 
    { 
     if(direct.mkdir()) 
      { 
      //directory is created; 
      } 

    } 

上面的代码是创建名称为“.test”的文件夹(SD卡中),然后保存你的数据(文件,视频..无论)在这个文件夹中的用户不能访问它。

如果您在内部存储器中创建文件夹,然后如果用户清除您的应用程序的数据,那么该文件夹可能会EMPTY

+0

我通过创建.test获得了什么? – Zookey 2013-03-22 11:49:13

+1

如果用户在使用SD卡读卡器的计算机上查看SD卡,该怎么办?即使该文件夹隐藏在那里,用户也可以在窗口中启用“查看隐藏的文件和文件夹”选项,并将看到这些文件。这不是一个好的安全。 – 2014-09-23 19:57:44

+0

如何做到这一点? – Sakthi 2016-06-30 09:05:22

4

这是在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(); 
} 
+0

这是用于加密和解密文本文件吗?是否可以加密图像或视频? – Zookey 2013-03-22 11:54:24

+0

是的,我已经使用它。它为我工作。 – Hasmukh 2013-03-22 11:55:15

+0

你是从服务器下载视频? – Hasmukh 2013-03-22 11:55:55

相关问题