Reputation: 2207
I am trying to save file into internal memory and also that should be readable by my application only. till now I have tried this.
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();
}
}
but this code snippet is throwing FileNotFound exception. Please suggest me on the same. Thanks in advance
Upvotes: 0
Views: 56
Reputation: 40347
This is mixed up:
Environment.getExternalStorageDirectory()+""+getFilesDir()+"/.fkfk";
Environment.getExternalStorageDirectory
and the getFiledDir()
method of Context refer to two completely different locations - the first on the primary "External Storage" (which these days is typically a permanent part of the device), and the second in the app's private storage area.
You should use only one of these methods to discover a location that best fits your goal. You might consult Storage Options or Saving Files in the Android documentation to aid your decision. If you wish something to be used only by your app, you would typically want the Internal Storage - ie, getFilesDir().
Once you have chosen a location, you will need to make the rest of your code consistent with it.
Upvotes: 2