My program creates a new html file and sends it to the ftp server. To do this, I create a local file in the device's memory. Everything works if I create directories and a file manually. As soon as I tried to create directories and a file programmatically, I ran into a problem. If you open Explorer in android studio, the file is created and located in the right directory. But when you try to view the file through Windows Explorer, there is no directory and file. In this regard (as I assume) the file is not sent to the ftp server. I can't solve the problem for many days. I have read many articles on the topics of memory and writing files to them. Please help me. Thank you in advance. I'm new in android developing.
public void sendFileIntoFTP(FTPClient ftpClient, String codeRes) throws URISyntaxException, IOException {
@SuppressLint("SdCardPath") String sourceFilePath = Environment.getExternalStorageDirectory() + File.separator + "timetracker" + File.separator + getYear() + File.separator + getMonth();
// Log.d("dirFTP", sourceFilePath);
String fileName = codeRes + ".html";
File sourceFile = new File(new URI(sourceFilePath));
Log.d("dirFTP", valueOf(sourceFile));
InputStream inputStream = new FileInputStream(sourceFile);
ftpClient.storeFile(fileName, inputStream);
if (ftpClient.storeFile(fileName, inputStream)) {
Log.d("dirFTP", "SUCCESS");
} else {
Log.d("dirFTP", "ERROR");
}
inputStream.close();
}
public void createLocalHTML(String codeRes, Context context) throws IOException {
@SuppressLint("SdCardPath") File internalDir = new File(Environment.getExternalStorageDirectory() + File.separator + "timetracker" + File.separator + getYear() + File.separator + getMonth());
Log.d("localDir", valueOf(internalDir));
String fileName = codeRes + ".html";
File internalFile = new File(internalDir, fileName);
Log.d("localDir", valueOf(internalFile));
if (internalFile.exists()) {
Log.d("localDir", "file.exists TRUE");
if (internalFile.canRead()) {
Log.d("localDir", "file.canRead TRUE");
} else { Log.d("localDir", "file.canRead FALSE"); }
// update file in future....
} else {Log.d("localDir", "file.exists FALSE");}
if (!internalDir.mkdirs()) {
internalDir.mkdirs();
Log.d("mLog", "Directory created in path: " + internalDir);
} else { return; }
internalFile.createNewFile();
FileWriter writer = new FileWriter(fileName, false);
writer.write(context.getAssets().open("dataTime.html").toString());
writer.close();
}
Log "localDir":
2023-11-14 10:44:44.567 6819-6945 localDir pid-6819 D /storage/emulated/0/timetracker/2023/11-2023
2023-11-14 10:44:44.567 6819-6945 localDir pid-6819 D /storage/emulated/0/timetracker/2023/11-2023/003.html
2023-11-14 10:44:44.572 6819-6945 localDir pid-6819 D file.exists TRUE
2023-11-14 10:44:44.573 6819-6945 localDir pid-6819 D file.canRead TRUE
UPD: My permissions:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />
I wanna see here my folder, which create programmatically: imageFromExplorer
Not an answer but an alternative for for your create function. That function will not always create a file as when the file already exists it just returns.
Also all those if-else's make it quite unreadable. Try to use only if's.
public boolean forceLocalHTML(String codeRes, Context context) throws IOException {
String fileName = getYear() + "/" + getMonth() + "/" + codeRes + ".html";
String subDir = "timetracker";
File file = new File(Environment.getExternalStorageDirectory(), subDir + "/" + fileName);
if ( file.exists() )
return true;
File dir = file.getParentFile();
if ( ! dir.exists())
{
if ( !dir.mkdirs() )
return false;
}
if ( ! file.createNewFile() )
return false;
FileWriter writer = new FileWriter(file.getAbsolutePath(), false);
writer.write(context.getAssets().open("dataTime.html").toString());
writer.close();
return true;
}