am working with Bitmap.decodeFile(pathname,bOptions), I wanted to include the file detected by the phone in the mean time am using this one
String path = Environment.getExternalStorageDirectory().toString() + "/Pictures/Temp Images";
this is only single one of string and I can't include in my array, I want to do is to pass the parameter to my method which is accepting String[] files which includes the pathFile + filename
ex: sd0/pictures/temp file/img1.jpeg
Supossing you have a variable where the file name is stored:
String path = Environment.getExternalStorageDirectory().toString() + "/Pictures/Temp Images" + File.separator + fileName;
public String getPath(String folderName, String fileName){
return Environment.getExternalStorageDirectory().toString() + folderName + File.separator + fileName;
}
using the method:
String path = getPath("/Pictures/Temp Images", "img1.jpeg");
or
String path = getPath("/Pictures/Temp Images", fileName);
Related
I have some code that is going to my local Downloads folder and getting the file size of a file, and I've converted it to human readable format using byteCountToDisplaySize(), but I need it to return 1 additional decimal point.
The code below returns 13 MB, but I need it to return 13.8 MB.
How can I do this?
public void seeTheSizeOfTheFile(String fileName String expectedFileSize) {
String filePath = System.getProperty("user.home") + File.separator + "Downloads" + File.separator + fileName;
File file = new File(filePath);
long actualFileSize = FileUtils.sizeOf(file);
String readableFileSize = FileUtils.byteCountToDisplaySize(actualFileSize);
}
I have the following code:
class Abcd{
//wired by spring to give the directory filePath ="/var/tmp/"
private String filePath;
public void myMethod(String id, String date){
filePath= filePath+ id+ "_" + date;
File f = new File(filePath);
if(f.exists){//Do something}
else{
System.out.println("File not found at file path:"+filePath);
}
}
}
The above code is behaving weird , intermittently the filePath contains all the files of directory /var/tmp/ . So , if /var/tmp directory contains two files called "id1_01012017" and "id2_10102017".
This is the intermittent output
File not found at file path:/var/tmp/id1_01012017id2_10102017
Am unable to figure out whats happening
The best way to do this is to maintain that filePath remains immutable. You will find that if you change this line:
filePath = filePath + id + "_" + date;
to the following:
String tempFilePath = filePath + id + "_" + date;
and operate on tempFilePath instead of filePath, your code will become thread-safe and work as expected.
How to rename a file by preserving file extension?
In my case I want to rename a file while uploading it. I am using Apache commons fileupload library.
Below is my code snippet.
File uploadedFile = new File(path + "/" + fileName);
item.write(uploadedFile);
//renaming uploaded file with unique value.
String id = UUID.randomUUID().toString();
File newName = new File(path + "/" + id);
if(uploadedFile.renameTo(newName)) {
} else {
System.out.println("Error");
}
The above code is changing the file extension too. How can I preserve it?
Is there any good way with apache commons file upload library?
Try to split and take only the extension's split:
String[] fileNameSplits = fileName.split("\\.");
// extension is assumed to be the last part
int extensionIndex = fileNameSplits.length - 1;
// add extension to id
File newName = new File(path + "/" + id + "." + fileNameSplits[extensionIndex]);
An example:
public static void main(String[] args){
String fileName = "filename.extension";
System.out.println("Old: " + fileName);
String id = "thisIsAnID";
String[] fileNameSplits = fileName.split("\\.");
// extension is assumed to be the last part
int extensionIndex = fileNameSplits.length - 1;
// add extension to id
System.out.println("New: " + id + "." + fileNameSplits[extensionIndex]);
}
BONUS - CLICK ME
I am trying to get a path of an image in my android device, such as:
/ storage/emulated/0/DCIM/Camera/NAME.jpg
and just trying to grab the image name, but i can.
I am trying with ...
String s = imagePath;
Where the route imagePath
s = s.substring (s.indexOf ("/") + 1);
s.substring s = (0, s.indexOf () ".");
Log.e ("image name", s);
it returns me :
storage/emulated/0/DCIM/Camera/NAME.jpg
and i only want
NAME.jpg
You need String.lastIndexOf():
String imagePath = "/path/to/file/here/file.jpg";
String path = imagePath.substring(imagePath.lastIndexOf('/') + 1);
You can do something like that:
File imgFile = new File(imagePath);
String filename = imgFile.getFilename();
This saves you a lot of hassle when you want to use your application cross-platform, because on Linux you have "/" as path delimiters and "\" on Windows
In case, if you are dealing with File object, then you can use its predefined method getName().
i.e.:
File mFile = new File("path of file");
String filename = mFile.getName();
In Java, I have a File object representing a folder:
String folderName = "/home/vektor/folder";
File folder = new File(folderName);
Now I want to create another File representing a file in this folder. I want to avoid doing a string concatenation like this:
String fileName = "test.txt";
File file = new File(folderName + "/" + fileName);
Because if I go deeper in creating this structure, I will come up with something like this:
File deepFile = new File(folderName + "/" + anotherFolderName + ... + "/" + fileName);
I would instead like to do something like
File betterFile = folder.createUnder(fileName);
Or even:
File otherFile = SomeFileUtils.createFileInFolder(folder, fileName);
Do you know of such solution?
Note: It's quite OK to use "/" because Java will translate it to "\" for Windows, but it is not clean - I should use something like "file.separator" from System.getProperties().
Look at the Javadoc for File and you will see that the constructor takes a File object as parent.
Use the following form:
File deepFile = new File(folder, fileName);
I would use
String folderName =
String fileName =
File under = new File(folderName, fileName);
or
File folderFile =
String fileName =
File under = new File(folderFile, fileName);
simple as that ;)