Selection creating New Folder In Root/sdcard/storage - java

I am allowing user to choose folder before downloading files.
This is what is did to give user an option to select folder.
private void pathlist(String basepath) {
File folderlist = new File(basepath);
File[] folders = folderlist.listFiles();
Log.e("Base", basepath);
for (File folder : folders) {
if (folder.isDirectory()) {
folist.add(folder.getName());
}
}
foladap = new Foladap(DownloadFolderOptions.this, folist);
listView.setAdapter(foladap);
}
where basepath = Environment.getExternalStorageDirectory().getAbsolutePath();
using above I get list of folders at Internal Memory.
But problem is when I select any folder like Movies, the final selected is
/storage/emulated/0/Movies
and System create a new folder at selected path instead of folder shown in list.
How to handle this.?
pls guide.
if I select Movies folder using Asus File Manager, the path shows as Root/Sdcard/Movies. what is this path and how it is different then what I selected as got after selection.

Related

How to delete file in android?

I have used the android internal storage to save a file for my application.
Ex
File rootFolder = context.getFilesDir();
File albumIdFolder = new File(rootFolder,getAlbumId());
Basicaly i want to delete the folder
So i tried with
File rootFolder = context.getFilesDir();
File albumIdFolder = new File(rootFolder,getAlbumId());
albumIdFolder.delete()
but this not working not deleting folder
I readed this answares but not worked in my case please help me solve this issue i am not getting where i am going wong.
Delete file from internal storage
How to delete internal storage file in android?
Edit 2
Ex ^(folder hierarchy)
data
user
0
packageName
files
155775346846131
otherData
i want to delete 155775346846131 folder
You can delete files with folder like as below,
void deleteFiles(Context context) {
File rootFolder = context.getFilesDir();
File fileDir = new File( rootFolder,getAlbumId());
if (fileDir.exists()) {
File[] listFiles = fileDir.listFiles();
for (File listFile : listFiles) {
if (!listFile.delete()) {
System.err.println( "Unable to delete file: " + listFile );
}
}
}
rootFolder.delete();
}
Source : How to delete a whole folder and content?
Don't forgot to give Storage permission.
You can delete files and folders recursively like this:
public void deleteFolderRecursive(File fileOrDirectory) {
if (fileOrDirectory.isDirectory()) {
for (File child : fileOrDirectory.listFiles()) {
deleteFolderRecursive(child);
}
}
fileOrDirectory.delete();
}

How do I create a String array with its elements being all the png files located in a resource folder?

I am trying to create a method that searches inside a folder for .png files and returns a String array with the respective path to each file. It must look inside a resource folder placed NOT in the src, but in project.
The following code works when running from within Eclipse:
// Analyzes specified folder and returns a file array
// populated with the .png files found in that folder
private File[] imageReader(String filePath) {
File folder = new File(filePath);
return folder.listFiles (new FilenameFilter() {
public boolean accept(File filePath, String filename)
{ return filename.endsWith(".png"); }
});
}
// Converts the file array into a string array
private String[] listPngFiles(String filePath) {
File[] imagesFileArray = imageReader(filePath); // file array
String[] imagesStringArray = new String[imagesFileArray.length];
for(int i=0; i<imagesFileArray.length; i++) {
imagesStringArray[i] = "" + imagesFileArray[i];
imagesStringArray[i] = imagesStringArray[i].substring(6); // discards "/images" from directory string
}
return imagesStringArray;
}
However it is not working when I run the exported executable JAR file. This is my current project setup:
.
I have tried the following code but it did not work either:
ClassLoader classLoader = getClass().getClassLoader();
File folder = new File(classLoader.getResource(filePath).getFile());
The reason I am doing this is because I want to have a JButton display an icon chosen from one of the sub folders inside the images resource folder. My JButton already has the following code:
.setIcon(new ImageIcon(GameLogic.class.getResource(**insert listPngFiles array element here**)));
Your help on the matter would be greatly appreciated.
You can achieve this by using Reflections, take a look at getResources

Copy one file from a folder to another folder in java

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:

Copy image to new directory and rename - Java

So far I have a list of images and I want to rename them based on information I get from a database.
List of images:
IBImages = ["foo1", "foo2", "foo3"]
private static void buildTheme(ArrayList<String> IBImages) {
String bundlesPath = "/a/long/path/with/dest/here";
for (int image = 0; image < IBImages.size(); image++) {
String folder = bundlesPath + "/" + image;
File destFolder = new File(folder);
// Create a new folder with the image name if it doesn't already exist
if (!destFolder.exists()) {
destFolder.mkdirs();
// Copy image here and rename based on a list returned from a database.
}
}
}
The JSON you get from the database might look something like this. I want to rename the one image that I have to all of the names in the list of icon_names
{
"icon_name": [
"Icon-40.png",
"Icon-40#2x.png",
"Icon-40#3x.png",
"Icon-Small.png",
"Icon-Small#2x.png",
]
}
You can't have into directory few files with same name at once. You need to either copy your file once and rename it, or create empty file with new name and copy bits from original file into it. Second approach is quite easy with Files class and its copy(source, target, copyOptions...) method.
Here is simple example of copying one file located in images/source/image.jpg to new files in image/target directory while giving them new names.
String[] newNames = { "foo.jpg", "bar.jpg", "baz.jpg" };
Path source = Paths.get("images/source/image.jpg"); //original file
Path targetDir = Paths.get("images/target");
Files.createDirectories(targetDir);//in case target directory didn't exist
for (String name : newNames) {
Path target = targetDir.resolve(name);// create new path ending with `name` content
System.out.println("copying into " + target);
Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
// I decided to replace already existing files with same name
}

Android: path of assets folder for File()?

I have some files in the assets folder of my project, and I want to list them, so I put this in my code:
File dir = new File("com.packagename/assets/fonts");
File[] fileList = dir.listFiles();
Which path should I put to make it work?
I want it so users could install new fonts (I don`t know how to do this yet) so I need to list all the fonts in the folder, including post-installed fonts. If there is any other solutions, please share.
Assets and resources are accessible using file:///android_asset and file:///android_res.
But in this case you will want to do something like this :
Resources res = getResources()
AssetManager am = res.getAssets();
String fileList[] = am.list(dirFrom);
if (fileList != null) {
for ( int i = 0;i<fileList.length;i++) {
Log.d("",fileList[i]);
}
}

Categories