As you can see in ss, some apps have created their own folders in my phone root folder.
So that means it's possible, but I couldn't find a way to do this.
Tried this but didn't work,
File folder = new File("/myfolder");
boolean success = true;
if (!folder.exists()) {
success = folder.mkdir();
}
You need to get put the path to sdcard first, like this:
File folder = new File(Environment.getExternalStorageDirectory(), "myfolder");
folder.mkdir();
You don't need to check if folder exists, as mkdir() will just do nothing (and return false) in that case.
Java.IO.File folder = new Java.IO.File(
Android.OS.Environment.ExternalStorageDirectory, "dalisto/cache");
folder.Mkdir ();
Related
Here is my code:
String dir = getFilesDir().getAbsolutePath();
File myFile = new File(dir+"/file.apk");
if (myFile.exists())
{
textView.setText("File exists.");
}
else
{
textView.setText("File does not exist.");
}
myFile.exists() is false. I do not know why. The file exists and it is located in the directory.
When I solve the problem, I'll try this:
Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
intent.setData(Uri.fromFile(myFile));
intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Can somebody help? Why it does not see the file?
UPDATE:
It's really strange. If I use code:
if (myFile.exists())
{
textView.setText("it exists");
}
else
{
textView.setText(myFile.getAbsolutePath());
}
, it goes to 'else' and shows the path to the file which 'does not exist'.
Thanks to greenapps:
"Please click in Astro app left to the word Primary on the up arrow to see the real path. /Primary/ does not exist on an Android device. It's an Astro invention. And Astro shows external memory with Primary. And take a better file explorer like ES File Explorer to inform you about real paths"
I used direct path I found using Astro (modified string dir to '/sdcard/data/data/...").
Try using this code:
String dir = getFilesDir().getAbsolutePath();
boolean fileExists = (new File(dir + "/file.apk")).isFile();
if (fileExists)
{
// your file exists
}
else
{
// your file does not exist
}
If you construct the file with the 2-arg constructor, you can avoid adding a system-dependent path separator character. Like this:
File myFile = new File(dir, "file.apk");
I have a existing folder (Old Folder name : xyz) in Sdcard, Whenever I try to rename this folder (New Folder name : .xyz) using toRename(). It return false and create a new folder (name : .xyz). Old Folder (name : xyz) is also visible in sdcard.
How to rename the existing folder to make a that Folder hidden in Android?
String dir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/xyz";
File file = new File(dir);
StringdirHide = Environment.getExternalStorageDirectory().getAbsolutePath() + "/.xyz";
File fileHide = new File(dirHide);
if (!file.exists() && !fileHide.exists())
{
fileHide.mkdir();
}
else if(file.exists())
{
file.toRename(fileHide);
}
The method to rename is renameTo. The following code should work. Tell me if you face any problems.
String dir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/xyz";
File file = new File(dir);
String dirHide = Environment.getExternalStorageDirectory().getAbsolutePath() + "/.xyz";
File fileHide = new File(dirHide);
if (file.exists() && !fileHide.exists()) {
file.renameTo(fileHide);
} else if(!file.exists()) {
fileHide.mkdir();
}
#Akashsingla19 I think problem is the folder u want to rename is not exist run following code Twice hope you will get your answer
if (!file.exists())
{
file.mkdir();
}
else if(file.exists())
{
file.renameTo(fileHide);
}
In your code you are using some toRename() method, which i couldn't found anywhere in File class in android. Actual method of File class in android to rename folders and files is renameTo(). Check this method and try to use it and revert please.
Thanks.
I'm using the Java CIFS Client Library but facing the problem and problem is copyTo function is not working.
I have one folder which contains files. I want to read these files to other network path.
String path1 = "//MACHINE-NAME/SHARE-FOLDER"
NtlmPasswordAuthentication auth = new
NtlmPasswordAuthentication(DOMAIN;USERNAME:PASSWORD)
SmbFile readFolder = new SmbFile("smb://MACHINE-NAME/SHARE-FOLDER/",auth)
This is working fine.
Then i have another network path and define like this and ShareFolder2 is have the read/write access to 'everyone' user.
String path2 = "//MACHINE-NAME/SHARE-FOLDER2"
NtlmPasswordAuthentication auth = new
NtlmPasswordAuthentication(DOMAIN;USERNAME:PASSWORD)
SmbFile destinationFolder = new SmbFile("smb://MACHINE-NAME/SHARE-FOLDER2/",auth)
ArrayList<SmbFile> readFiles = readFolder?.listFiles()
for(file in readFiles ){
file.copyTo(destinationFolder)
}
If you wanted to copy a file from one shared location to another shared location. You can this like this
ArrayList<SmbFile> readFiles = readFolder?.listFiles()
for(file in readFiles ){
String name = file.properties.getKey("name")
destinationFolder = new SmbFile(foldersInfo?.destinationFolder+"/"+name,auth)
destinationFolder.createNewFile()
file.copyTo(destinationFolder)
}
The file which you want to copy that file must be in the destination folder.
First we will create a file with same name in the destination folder then copy to that folder
I am trying to copy a file form a folder to another folder
i have tried what was suggested in other posts but i have not been successful
Copying files from one directory to another in Java
this has not worked for me
the file is C:/Users/win7/Desktop/G1_S215075820014_T111_N20738-A_D2015-01-26_P_H0.xml
the destination folder is C:/Users/win7/Desktop/destiny
this is the copy code
String origen = "C:/Users/win7/Desktop/G1_S215075820014"
+"_T111_N20738-A_D2015-01-26_P_H0.xml";
String destino = "C:/Users/win7/Desktop/destiny";
private void copiarArchivoACarpeta(String origen, String destino) throws IOException {
Path FROM = Paths.get(origen);
Path TO = Paths.get(destino);
CopyOption[] options =
new CopyOption[] {StandardCopyOption.REPLACE_EXISTING,
StandardCopyOption.COPY_ATTRIBUTES };
java.nio.file.Files.copy(FROM, TO, options);
}
Try:
java.nio.file.Files.copy(FROM, TO.resolve(FROM.getFileName()),
StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);
Because the second parameter must be a Path to a file that not yet exists.
Just like the docu sais:
I want to programmatically access a specific file which will be included in my project folder. Is there a way to do this? If so, where in my project folder do I put the file, and what is some simple code to get its file path?
private void saveFileToDrive() {
Thread t = new Thread(new Runnable() {
#Override
public void run() {
try {
java.io.File spreadsheet = new java.io.File("Untitled spreadsheet.xlsx");
String filePath = spreadsheet.getAbsolutePath();
System.out.println("file path is"+filePath);
URL fileURL = getClass().getClassLoader().getResource("Untitled spreadsheet.xlsx");
String filePath2 = fileURL.getPath();
System.out.println("file path2 is"+filePath2);
java.io.File fileContent = new java.io.File(filePath);
FileContent mediaContent = new FileContent("application/vnd.ms-excel", fileContent);
File body = new File();
body.setTitle(fileContent.getName());
body.setMimeType("application/vnd.ms-excel");
File file = service.files().insert(body, mediaContent).setConvert(true).execute();
if (file != null) {
showToast("File uploaded: " + file.getTitle());
}
else
;
} catch (UserRecoverableAuthIOException e) {
startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
} catch (IOException e) {
e.printStackTrace();
}
}
});
t.start();
}
Put the file in root folder of your project. Then get the File URL, Path and other details as:
File file = new File("test.txt");
String filePath = file.getAbsolutePath();
EDIT: Alternate way (if the file is in your classpath e.g. put the file in "src" folder, and make sure its moved in "bin" or "classes" folder after compilation):
URL fileURL = getClass().getClassLoader().getResource(fileName);
String fileName = fileURL.getFile();
String filePath = fileURL.getPath();
This depends a lot on what type of file you want to access. You can put the file in either assets or an appropriate subdirectory of res (see Difference between /res and /assets directories).
So you want to access a file internal to your app; and you want to do so directly, rather, that is, from an Android Context (and then with a [android.|<package_name>.]R.<resource_type>.<resource_name>).
You have two choices as to location: the res/raw folder or assets/ folder (outside of the res parent).
To choose between the two note from https://developer.android.com/guide/topics/resources/providing-resources.html
Arbitrary files to save in their raw form. To open these resources with a raw InputStream, call Resources.openRawResource() with the resource ID, which is R.raw.filename.
However, if you need access to original file names and file hierarchy, you might consider saving some resources in the assets/ directory (instead of res/raw/). Files in assets/ aren't given a resource ID, so you can read them only using AssetManager.
To access a file in res/raw/ directly rather, that is, from an Android Context (and then with a [android.|<package_name>.]R.<resource_type>.<resource_name>) you can do something like this:
File file = new File("app/src/main/res/raw/country_data_from_world_bank.xml");
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));