How to make universal image loader get image from sd card dynamically - java

i get an example from here
https://github.com/nostra13/Android-Universal-Image-Loader
In the Constants.java is for the image source link. The thing that i want is scan the specific folder in sdcard, then get all image url in dynamic.
Not one by one type the image url.
Not this type one by one. ---> "file:///sdcard/Universal Image Loader ##&=+-_.,!()~'%20.png"
Here is my code. But it can't work. Any solution about this? Thanks.
public class Constants {
private String[] mFileStrings;
private File[] listFile;
static List<String> test = new ArrayList<String>();
public void getFromSdcard()
{
System.out.println("testing here can");
File file= new File(android.os.Environment.getExternalStorageDirectory(),"file:///sdcard/bluetooth/"); //sdcard/bluetooth
if (file.isDirectory())//if files is directory
{
listFile = file.listFiles();
mFileStrings = new String[listFile.length];
for (int i = 0; i < listFile.length; i++)
{
mFileStrings[i] = listFile[i].getAbsolutePath();//mFileStrings with uri of images
test.add(listFile[i].getAbsolutePath());
}
}
}
static String simpleArray = test.toString();
//String img = mFileStrings.toString();
public static final String[] IMAGES = new String[] {simpleArray};
private Constants() {
}

To get image from sdcard, use following code.
String fileName= listFile[i];
fileName = fileName.replace(':', '/');
fileName = fileName.replace('/', '_');
String loadURL="file://"+Environment.getExternalStorageDirectory()+"/"+folder+"/"+fileName;
Now, you have absolute path of your image in "loadURL".
if (file.isDirectory())//if files is directory
{
listFile = file.listFiles();
mFileStrings = new String[listFile.length];
for (int i = 0; i < listFile.length; i++)
{
String fileName=listFile[i];
fileName = fileName.replace(':', '/');
fileName = fileName.replace('/', '_');
String loadURL="file://"+Environment.getExternalStorageDirectory()+"/"+folder+"/"+fileName;
test.add(loadURL);
}
}

Related

How to iterate through multiple folders and compare pdfs stored in it using Java

I am trying to compare PDF files stored in different folders one by one below is the code I wrote in it when using that when it compares the second PDF file(file 22)of folder (data) its taking the first PDF from the other folder to compare can any one help
String object ="D:\\test.pdf";
String data ="D:\\test2.pdf";
Log.info("PDF Documents has been Verified");
// boolean isEquals = false;
try{
String result =Constants.Path_Result+DriverScript.s;
File[] file10 = new File(object).listFiles(File::isFile);
for (File file1: file10) {
String fileExtension = file1.getName().split("\\.")[file1.getName().split("\\.").length - 1];
if (fileExtension.toLowerCase().equals("pdf")) {
file12 =file1.getAbsolutePath();
System.out.println(file12);
File[] file121 = new File(data).listFiles(File::isFile);
for (File file2: file121) {
String fileExtension1 = file2.getName().split("\\.")[file2.getName().split("\\.").length - 1];
if (fileExtension1.toLowerCase().equals("pdf")) {
String file22 =file2.getAbsolutePath();
new de.redsix.pdfcompare.PdfComparator(file12, file22).compare().writeTo(result+"//"+file2.getName());
}}}}
}catch(Exception e){
System.out.println(e.getMessage());
Log.error("Not able to Close the Browser --- " + e.getMessage());
DriverScript.bResult = false;
}
}```
Hi use the below java code it will work
(What i did was combined two of your for loop that you have written in to one so that both the folders will get iterated at the same time.)
String path1 = "D:\\newfolder\\";
String path2 = "D:\\newfolder1\\";
String result="D:\\result\\";
File folder1 = new File(path1);
File folder2 = new File(path2);
File[] listOfFiles1 = folder1.listFiles();
File[] listOfFiles2 = folder2.listFiles();
ArrayList<String> fileNames1 = new ArrayList<String>();
ArrayList<String> fileNames2 = new ArrayList<String>();
for ( int i = 0,j = 0; i < listOfFiles2.length&& j < listOfFiles1.length; i++,j++)
if (listOfFiles2[i].isFile()&&listOfFiles1[j].isFile())
{ fileNames2.add(listOfFiles2[i].getName().split("\\.")[listOfFiles2[i].getName().split("\\.").length - 1]);
fileNames1.add(listOfFiles1[j].getName().split("\\.")[listOfFiles1[j].getName().split("\\.").length - 1]);
String str = fileNames1.toString();
str.toLowerCase().equals("pdf");
String file01 =listOfFiles1[j].getName();
file12 =listOfFiles1[j].getAbsolutePath();
String str1 = fileNames2.toString();
str1.toLowerCase().equals("pdf");
file0 =listOfFiles2[i].getName();
file22 =listOfFiles2[i].getAbsolutePath();
new de.redsix.pdfcompare.PdfComparator(file12,file22).compare().writeTo(result+"//"+file0);
}

How to store list of files and folder in list or set using java?

I have return code to retrieve a list of all filepaths within a directory but I'm only getting the contents of the last folder. I have two folders, each has 3 files.
Here is my code:
import java.io.*;
import java.util.*;
class Filedirexts
{
public static void main(String[] args) throws IOException
{
String Dirpath = "E:/Share/tstlpatches";
String fieldirpath ="";
File file = new File(Dirpath);
List<String> strfilelst = new ArrayList<String>();
strfilelst = Filedirexts.getsubdir(file);
System.out.println(strfilelst.size());
for(int i=0;i<strfilelst.size();i++)
{
fieldirpath = strfilelst.get(i);
System.out.println("fieldirpath : "+fieldirpath);
}
}
public static List<String> getsubdir(File file) throws IOException
{
File[] filelist = file.listFiles();
List<String> strfileList = new ArrayList<String>();
System.out.println("filelist" + filelist.length);
for (int i=0; i< filelist.length ; i++)
{
if(filelist[i].exists())
{
if(filelist[i].isFile())
{
file = filelist[i];
System.out.println( " fileeach file : "+fileeach.getAbsolutePath());
strfileList.add(file.getAbsolutePath());
}
else if (filelist[i].isDirectory())
{
file = filelist[i];
System.out.println( " fileeach Directory : "+fileeach.getCanonicalPath());
strfileList = Filedirexts.getsubdir(file);
strfileList.add(file.getCanonicalPath().toString());
}
}
}
return strfileList;
}
}
This is my folder structure:
MainPath E:\Share\tstlpatches which is used in code itself
E:\Share\tstlpatches\BE
E:\Share\tstlpatches\BE\graphical
E:\Share\tstlpatches\BE\graphical\data1.txt
E:\Share\tstlpatches\BE\graphical\data2.txt
E:\Share\tstlpatches\BE\graphical\data3.txt
E:\Share\tstlpatches\BE\test
E:\Share\tstlpatches\BE\test\1.txt
E:\Share\tstlpatches\BE\test\2.txt
E:\Share\tstlpatches\BE\test\readme.txt
I'm only getting
E:\Share\tstlpatches\BE
E:\Share\tstlpatches\BE\test
E:\Share\tstlpatches\BE\test\1.txt
E:\Share\tstlpatches\BE\test\2.txt
E:\Share\tstlpatches\BE\test\readme.txt
If I use the normal method, it works fine, but when I use with the list I'm only getting the constents of the last folder.
What do I need to do to make the code work properly?
Your recursive method is incorrectly creating a new ArrayList<String> in each call and returning this new ArrayList in each invocation. This is why you are only seeing the contents of the last call.
Here's how to fix this:
1) Change the getsubdir to be void and pass in the List as a parameter.
public static void getsubdir(File file, List<String> strfileList) throws IOException
{
File[] filelist = file.listFiles();
System.out.println("filelist " + filelist.length);
for (int i=0; i< filelist.length ; i++)
{
if(filelist[i].exists())
{
if(filelist[i].isFile())
{
file = filelist[i];
System.out.println( " fileeach file : "+file.getAbsolutePath());
strfileList.add(file.getAbsolutePath());
}
else if (filelist[i].isDirectory())
{
file = filelist[i];
System.out.println( " fileeach Directory : "+file.getCanonicalPath());
// Note: Since you want the directory first in the list,
// add it before the recursive call
strfileList.add(file.getCanonicalPath().toString());
Filedirexts.getsubdir(file, strfileList);
}
}
}
}
2) Change the way you call it from main:
Instead of:
strfilelst = Filedirexts.getsubdir(file);
Just use:
Filedirexts.getsubdir(file, strfilelst);

Changing a string dynamically for file usage

I am trying to figure out how to make a string that I can use to create a file in my java program. I have tried many different ways of doing this, but nothing seems to do what I want.
Here is an example of what I am trying to do:
String fileExt = ".jpg"
for (int i = 0; i < someNumber; i ++){
fileExt = i + fileExt;
}
I want to output something like:
1.jpg
2.jpg
3.jpg
4.jpg
... and so on.
My intention is to use the changed string at every iteration in order to create a new file with it like so : File image = new File (fileExt;
List<File> files = new ArrayList<File>();
String fileExt = ".jpg"
for (int i = 0; i < someNumber; i ++){
File newfile = new File(i + fileExt);
files.add(newfile)
}
A few things here. filext should be absolute path, or the files will be created in your current directory. Keep file references in List for future use.
No matter what you do, the value of fileExt is going to be the last iteration of the loop. Maybe an array will do the trick?
String[] fileExt = new String[20];
for (int i = 0; i < fileExt.length; i++) {
fileExt[i] = i + ".jpg";
}
// view results
for (int i = 0; i < fileExt.length; i++) {
System.out.println(fileExt[i]);
}
// example
File image = new File(fileExt[0]);

How to split strings in Android?

I have implementing the android application for split the file path and get the actual file name in text-view. When I run the application it displays all the String name from file path. I want only one file name (introduction.ppt). How can I get this file name in text-view?
Here is my code:
String path = "/mnt/sdcard/coverted/introduction.ppt";
String[] strAfterSpilt = path.split("/");
for(int j = 0; j < strAfterSpilt.length; j++) {
if(strAfterSpilt[j].length() > 0) {
String strFinalSplit = strAfterSpilt[j];
System.out.println("strFinalSplit :-> " + strFinalSplit);
function_arg1.setText(strFinalSplit);
} else { break; }
}
And also I have to try this way of split the file path;
String[] strAfterSpilt = path.split("/");
for (String str : strAfterSpilt) {
System.out.println("str" + str.lastIndexOf(strAfterSpilt[i]));
}
String path = "/mnt/sdcard/coverted/introduction.ppt";
String fileName = path.substring(path.lastIndexOf("/")+1);
will do your job.
String[] tokens = path.split("/");
String fileName = tokens[tokens.length - 1];
Just try following code.. specifying the start and end position of required string.
String path = "/mnt/sdcard/coverted/introduction.ppt";
String fileName = path.substring(path.lastIndexOf("/"), path.length);
Try out following codes
public class MainActivity extends Activity {
String path = "/mnt/sdcard/coverted/introduction.ppt";
String fileName;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// To get the File name
fileName = path.substring(path.lastIndexOf("/")+1);
TextView remotefilePath =(TextView)findViewById(R.id.txtFileName);
remotefilePath.setText(fileName);
}
}

Java - How to list content of a directory?

I want to display the content of the directory /etc/yum.repos.d in Centos. Usually in this directory is stored information about repositories from which Centos downloads RPM packages.
What is the proper way to display the content of a directory in Java?
Best wishes
import java.io.File;
import java.util.Arrays;
public class Dir {
static int indentLevel = -1;
static void listPath(File path) {
File files[];
indentLevel++;
files = path.listFiles();
Arrays.sort(files);
for (int i = 0, n = files.length; i < n; i++) {
for (int indent = 0; indent < indentLevel; indent++) {
System.out.print(" ");
}
System.out.println(files[i].toString());
if (files[i].isDirectory()) {
listPath(files[i]);
}
}
indentLevel--;
}
public static void main(String args[]) {
listPath(new File("/etc/yum.repos.d"));
}
}
Here is an example :
File directory = new File("/etc/yum.repos.d");
File[] files = directory.listFiles();
File dir = new File("some_directory");
String[] filesAndSubdirs = dir.list();

Categories

Resources