Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android中保存、读取和保护应用程序目录的数据_Android_File_Security_Android Internal Storage - Fatal编程技术网

在android中保存、读取和保护应用程序目录的数据

在android中保存、读取和保护应用程序目录的数据,android,file,security,android-internal-storage,Android,File,Security,Android Internal Storage,我正在尝试将文件保存到内存中,并且该文件应该只能由我的应用程序读取。 到现在为止,我已经试过了 String filesave = Environment.getExternalStorageDirectory()+""+getFilesDir()+"/.fkfk"; File f = new File(filesave); if (!f.exists()){ f.mkdir(); } File sd = Environment.getExt

我正在尝试将文件保存到内存中,并且该文件应该只能由我的应用程序读取。 到现在为止,我已经试过了

  String filesave  =     Environment.getExternalStorageDirectory()+""+getFilesDir()+"/.fkfk";
    File f = new File(filesave);
    if (!f.exists()){
        f.mkdir();
    }
File sd = Environment.getExternalStorageDirectory();
    Log.e("files", Environment.getExternalStorageDirectory()+f.getAbsolutePath());
String path  = Environment.getExternalStorageDirectory()+"/.NEWFolders/hdfc0002.jpg";
    File toCopy = new File(path);
    if (toCopy.exists()){
        Log.e("ok","got the path");
        try{
            if (sd.canWrite()) {
                File source= toCopy;
                File destination=f;
                if (source.exists()) {
                    FileChannel src = new FileInputStream(source).getChannel();
                    FileChannel dst = new FileOutputStream(destination).getChannel();
                    if (dst != null && source != null) {
                        dst.transferFrom(src, 0, src.size());
                    }
                    if (src != null) {
                        src.close();
                    }
                    if (dst != null) {
                        dst.close();
                    }
                }else {
                    FileChannel src = new FileInputStream(source).getChannel();
                    FileChannel dst = new FileOutputStream(destination).getChannel();
                    if (dst != null && source != null) {
                        dst.transferFrom(src, 0, src.size());
                    }
                    if (src != null) {
                        src.close();
                    }
                    if (dst != null) {
                        dst.close();
                    }
                }
            }
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }

    }
但此代码段正在引发FileNotFound异常。 请给我同样的建议。 提前感谢

这是混淆的:

Environment.getExternalStorageDirectory()+""+getFilesDir()+"/.fkfk";
Environment.getExternalStorageDirectory
getFiledDir()
上下文方法指的是两个完全不同的位置——第一个在主“外部存储”(现在通常是设备的永久部分)上,第二个在应用程序的私有存储区域

您应该只使用其中一种方法来发现最适合您的目标的位置。您可以参考Android文档中的或,以帮助您做出决定。如果您希望某些内容仅由应用程序使用,则通常需要内部存储-即getFilesDir()

一旦您选择了一个位置,您就需要使代码的其余部分与之保持一致。

这是混淆的:

Environment.getExternalStorageDirectory()+""+getFilesDir()+"/.fkfk";
Environment.getExternalStorageDirectory
getFiledDir()
上下文方法指的是两个完全不同的位置——第一个在主“外部存储”(现在通常是设备的永久部分)上,第二个在应用程序的私有存储区域

您应该只使用其中一种方法来发现最适合您的目标的位置。您可以参考Android文档中的或,以帮助您做出决定。如果您希望某些内容仅由应用程序使用,则通常需要内部存储-即getFilesDir()


一旦你选择了一个地点,您需要使其余代码与之保持一致。

我想在应用程序中使用私有数据目录。请详细说明。请参阅提供的android文档的链接。我想在应用程序中使用私有数据目录。请详细说明。请参阅安卓文档的链接假如