Android.Write file to sdcard - android-sdcard

I know this question was asked hundred times but for some reason accepted answers don't work for me.
I can write to device internal memory with this code:
File root = context.getExternalFilesDir(null);
root = new File(root, DATA_FOLDER);
if (!root.exists())
root.mkdir();
File pack = new File(root, "pack???");
if (!pack.exists())
pack.mkdir();
File progress = new File(pack, "save.dat");
try {
progress.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
//write file goes here
I can see folder structure in the app's folder
However if I change root to
File root=Environment.getExternalStorageDirectory();
no folders on the sdcard are created.Also I tried with this code:
File progress = new File(Environment.getExternalStorageDirectory(), "TestFolder");
if (!progress.exists()) {
progress.mkdir();
}
No success as well.
Manifest contains permission
android.permission.WRITE_EXTERNAL_STORAGE
UPD:Actually the last code does create new folder but this folder is in internal memory while I need it to be on sdcard

Related

Unable to create sheet when using SXSSFWorkbook in docker with apache poi

When executing following code in docker context, the page just hangs and I do not get any exceptions either:
File file = fileService.createTempFile('some-file.xlsx')
log.info("CREATED EXCEL FILE : ${file.path}")
Workbook workbook = new SXSSFWorkbook(SXSSFWorkbook.DEFAULT_WINDOW_SIZE)
log.info("WORKBOOK CREATED")
workbook.createSheet(WorkbookUtil.createSafeSheetName('TEST'))
log.info("SHEET CREATED")
FileOutputStream fos = new FileOutputStream(file)
workbook.write(fos)
//workbook.dispose()
fos.close()
I can see the file is craeted and also see WORKBOOK CREATED log generated but nothing after that.
This happens only when running the code in the docker container and not when I run the app outside of docker context.
I am using poi 4.1.2 and jdk 11.0.16.
UPDATE:
I have read on other posts about the temp directory not existing or not being writable. Just to test this out, I went to the node and created pre-defined folder with all read write permissions as well and updated the code to point SXXF to write to that temp folder as well :
try {
//THIS FOLDER ALREADY EXISTS IN NODE AND HAS ALL PERMS
String path = '/usr/local/tomcat/temp/reporting-service-16769203353988068118/'
File tmpdir = new File(path)
log.info("TMP DIR IS ${tmpdir.path}")
File file = new File("${tmpdir.path}/surveyQuestionExport.xlsx")
log.info("CREATED EXCEL FILE : ${file.path}")
TempFile.setTempFileCreationStrategy(new DefaultTempFileCreationStrategy(tmpdir))
Workbook workbook = new SXSSFWorkbook(new XSSFWorkbook(), SXSSFWorkbook.DEFAULT_WINDOW_SIZE)
log.info("WORKBOOK CREATED")
workbook.createSheet(WorkbookUtil.createSafeSheetName('TEST'))
log.info("SHEET CREATED")
FileOutputStream fos = new FileOutputStream(file)
workbook.write(fos)
workbook.dispose()
fos.flush()
fos.close()
return file
} catch (Exception ex) {
log.error("EXPORT FAILED", ex)
}
But still same issue.

Node FS not finding folder

Using Node, I create a folder and then have a file in that folder. I created a function to delete it, but it absolutely refuses to find the folder.
Here's my function:
function deleteFile(path) {
if( !fs.existsSync(path) ) {
setTimeout(deleteFile(path), 500)
} else {
fs.readdirSync(path).forEach(function(file){
var curPath = path + "/" + file;
if(fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
}
It will continue to recurse until it hits maximum call stack and crash, but the folder exists LONG before that happens. As you can see, there exists both the folder and the file inside of it. Could someone please help me fix this?
If anyone else comes across this issue, I figured it out. When the folder is created, it gives the incorrect permissions. I used fs.chmod to change permissions beforehand, and that fixed it.

Multiple .zip files extracting on the app directory c#

I have an auto update program for game launcher and I want to know how I can auto unzip all zip files on the same folder with launcher. (Multiple zip files are downloaded). I am a newbie when it comes to coding.
I need to change the following code for extracting all .zip files in the current folder where the app is.
using (var zip = Ionic.Zip.ZipFile.Read("desktop.zip"))
{
zip.ExtractAll(Directory.GetCurrentDirectory(), ExtractExistingFileAction.OverwriteSilently);
}
Try using Reflection:
using (var zip = Ionic.Zip.ZipFile.Read("desktop.zip"))
{
string exeFullPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
string exePath = System.IO.Path.GetDirectoryName(exeFullPath);
zip.ExtractAll(exePath
, ExtractExistingFileAction.OverwriteSilently);
}
#vendettamit is give me the correct answer. If someone need im going to post his code.
foreach (var file in Directory.EnumerateFiles("<directory path>", "*.zip"))
{
using (ZipFile zip = ZipFile.Read(file))
{
foreach (ZipEntry zipFiles in zip)
{
zipFiles.Extract(currentpath, true);
}
}
}

XMLDocument.Load() Does not load the file while running test project

I have a test case which validates an XML file.
When using VS2010 with .Net 3.5 framework, the below code works perfectly fine, i am able to load the XML file. My file is location is Source folder of the application.
XmlDocument doc = new XmlDocument();
try
{
doc.Load("Terms_and_Conditions.xml");
XmlNode node;
XmlElement root = doc.DocumentElement;
//Select and display the value of the element.
node = root.SelectSingleNode(NodeSelection);
return node.InnerText;
}
catch (Exception ex)
{
throw ex;
}
Solution Folder:
When I run the same code in .Net 4.6.1, file path resolves to
C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\Common7\\Terms_and_Conditions.xml
Anyone have an idea why this issue with .Net 4.6.1
This happens because your unit tests run in the test runner, which is located in the Common7 folder and not in your projects bin folder. Since you specify a relative path to the xml file, the program will look for the file in the current folder, which is the Common7 folder.
Thanks everyone for the quick help.
Here is how I solved the problem with above link.
XmlDocument doc = new XmlDocument();
try
{
var path = System.IO.Directory.GetParent(System.IO.Directory.GetParent(TestContext.CurrentContext.TestDirectory).ToString());
doc.Load("Terms_and_Conditions.xml");
XmlNode node;
XmlElement root = doc.DocumentElement;
//Select and display the value of the element.
node = root.SelectSingleNode(NodeSelection);
return node.InnerText;
}
catch (Exception ex)
{
throw ex;
}

File server to SharePoint Online: folder to document library structure diff tool?

Do you know if there is any kind of tool (it would be cool if it is for free) that compares the file structure of a folder in a File Server to a SharePoint Online Document Library and tells you the difference?
The point is we are getting hands on a partially migrated File Server to SharePoint Online and the migration status of some folders is unknown to us. We already have a 3rd party migration tool to finish the migration but we need to know what is still pending to be uploaded. Any idea?
Thanks in advance.
We use sharegate for our migration. In your situation I might just run migrations on small sections with it set to "skip existing". That doesn't directly answer your question though.
In your case I would map both the File server and the SharePoint online instance as drives on a computer (using WebDAV) and hack together an app in your favorite language to compare the two directory structures.
Here is a java program that does that:
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
public class Launcher {
static PrintWriter fileOut;
public static void main(String[] args) throws IOException {
File fileStore = new File("N:\\"); //filestore dir top level
File sp = new File("M:\\");//sp directory top level (mapped)
File out = new File("C:\\Temp\\results.txt");
fileOut = new PrintWriter(out);
recurse(fileStore, sp);
fileOut.close();
}
private static void recurse(File dir1, File dir2)
{
File [] dirFiles;
if(dir2 == null)
dirFiles = new File[0];
else
dirFiles=dir2.listFiles();
File match;
for(File f1 : dir1.listFiles())
{
System.out.println(f1.getAbsolutePath());
match=null;
for(File f2:dirFiles)
if(f1.getName().equalsIgnoreCase(f2.getName()))
match=f2;
if(f1.isDirectory() && f1.canRead())
try{
recurse(f1,match);
}
catch(Exception e){}
else if (match == null)
fileOut.write(f1.getAbsolutePath()+"\r\n");
}
}
}
It writes to the given text file each file that is not in the 2nd directory structure.
You could run this code in an IDE like eclipse.

Resources