在 android 中保存、读取和保护应用程序目录的数据
saving, reading and securing data of apps directory in android
我正在尝试将文件保存到内部存储器中,并且该文件应该只能由我的应用程序读取。
到现在我已经试过了。
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
和 Context 的 getFiledDir()
方法指的是两个完全不同的位置——第一个在主 "External Storage" 上(现在通常是设备的永久部分) ,第二个在应用的私人存储区。
您应该只使用这些方法中的一种来发现最适合您目标的位置。您可以参考 Android 文档中的 Storage Options or Saving Files 来帮助您做出决定。如果您希望某些内容仅供您的应用程序使用,您通常需要内部存储 - 即 getFilesDir()。
选择位置后,您需要使其余代码与其保持一致。
我正在尝试将文件保存到内部存储器中,并且该文件应该只能由我的应用程序读取。 到现在我已经试过了。
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
和 Context 的 getFiledDir()
方法指的是两个完全不同的位置——第一个在主 "External Storage" 上(现在通常是设备的永久部分) ,第二个在应用的私人存储区。
您应该只使用这些方法中的一种来发现最适合您目标的位置。您可以参考 Android 文档中的 Storage Options or Saving Files 来帮助您做出决定。如果您希望某些内容仅供您的应用程序使用,您通常需要内部存储 - 即 getFilesDir()。
选择位置后,您需要使其余代码与其保持一致。