Let's say I have the following string
http://i2.kym-cdn.com/photos/images/original/000/748/132/b84.png
What would be the best way to extract b84.png from it? My program will be getting a list of image URLs and I want to extract the file name with its extension from each URL.
I would recommend using URI to create a File like this:
String url = "http://i2.kym-cdn.com/photos/images/original/000/748/132/b84.png";
URI uri = URI.create(url);
File f = new File(uri.getPath());
The uri.getPath() returns only the path portion of the url (i.e. removes the scheme, host, etc.) and produces this:
/photos/images/original/000/748/132/b84.png
You can then use the created File object to extract the file name from the full path:
String fileName = f.getName();
System.out.println(fileName);
Output of print statement would be:
b84.png
However if you are not at all concerned by the input format of the url(s) then the substring answers are more terse. I figured I would offer an alternative. Hope it helps.
Try,
String url = "http://i2.kym-cdn.com/photos/images/original/000/748/132/b84.png";
String fileName = url.subString(url.lastIndexOf("/")+1);
String[] urlArray=yourUrlString.split("/");
String fileName=urlArray[urlArray.length-1];
I think, string lastIndexOf method is the best way to extract file name
String str = "http://i2.kym-cdn.com/photos/images/original/000/748/132/b84.png";
String result = str.substring(str.lastIndexOf('/')+1,str.length());
The method perfect for you
public String Name(String url){
url=url.replace('\\', '/');
return url.substring(url.lastIndexOf('/')+1,url.length());
}
this method returns any filename of any directory...whitout errors because convert the "\" to "/" so never will have problems with diferent directories
You can try this-
String url = "http://i2.kym-cdn.com/photos/images/original/000/748/132/b84.png"
url = url.substring(url.lastIndexOf("."));
If you don't png instead of .png, just change the last line to this-
url = url.substring(url.lastIndexOf(".") + 1);
Related
Using JSOUP I parse an HTML page and I've found the image path, but now I need to obtain the image file name which is a part of the url path.
For example, this is the img src:
http://cdn-6.justdogbreeds.com/images/3.gif.pagespeed.ce.MVozFWTz66.gif
The file name is 3.gif.
What shall I use to obtain the name from the URL path? Perhaps regex?
I also have other url images:
http://cdn-1.justdogbreeds.com/images/**10.gif**.pagespeed.ce.gsOmm6tF7W.gif
http://cdn-4.justdogbreeds.com/images/**6.gif**.pagespeed.ce.KbjtJ32Zwx.gif
http://cdn-2.justdogbreeds.com/images/**8.gif**.pagespeed.ce.WAWhS-Qb82.gif
http://cdn-3.justdogbreeds.com/images/**7.gif**.pagespeed.ce.UKTkscU8uT.gif
Instead of regex you can use String.lastIndexOf() with String.substring().
String imgSrc = "http://cdn-1.justdogbreeds.com/images/10.gif.pagespeed.ce.gsOmm6tF7W.gif";
String imageName = imgSrc.substring(imgSrc.lastIndexOf("/") + 1);
imageName = imageName.substring(0, imageName.indexOf(".", 3));
System.out.println(imageName); // prints out 10.gif
This finds the last occurrence of forward slash ( / ), after which the image name starts. The rest of the string is the full image name. You want only the 10.gif bit, so the rest of Line 2 finds the next period after the image name.
You can use a regex replacement to get the value you need:
String filename = imgsrc.replaceAll("http://[^/]*justdogbreeds.com/images/([^/]*?\\.gif).*", "$1");
With the regex we match the whole URL, and capture the text right after the images/ and up to (including) the first .gif. The ([^/]*?\\.gif) matches 0 or more characters other than / as few as possible, and then .gif. If you have other extensions, you may either enumerate them in an alternation group (like ([^/]*?\\.(?:gif|jpe?g|png)), or use a more generic pattern [^.]+ (1 or more characters other than .):
String filename = imgsrc.replaceAll("http://[^/]*justdogbreeds.com/images/([^/]*?\\.[^.]+).*", "$1");
See IDEONE demo
String imgsrc = "http://cdn-1.justdogbreeds.com/images/10.gif.pagespeed.ce.gsOmm6tF7W.gif";
String filename = imgsrc.replaceAll("http://[^/]*justdogbreeds.com/images/([^/]*?\\.gif).*", "$1");
System.out.println(filename);
So I am making an app that lets users pick images from their gallery to display, as well as take photos from the camera.
The image is previewed via a bitmap that takes the string of the file path. When the user takes a photo the code below handles it (and works):
String path = mediaStorageDir.getPath() + File.separator + "IMG_"+ timeStamp + ".jpg"
File pictureFile = new File(path);
Bitmap bitmap = BitmapFactory.decodeFile(pictureFile.getAbsolutePath());
image1.setImageBitmap(bitmap);
The path is of the format like this:
/storage/emulated/0/Pictures/App/IMG_20150512_130719.jpg
However when I try to select from my library, my code is setup so that the string I am returning formatted like this:
file:////storage/emulated/0/Pictures/App/IMG_20150512_130719.jpg
Now obviously I can (and currently am) split the string and get the path to match the above, but I am curious if there is a standard way to convert between these to?
I saw Uri.parse() but that doesn't work because that returns a URI and I want a String that I can make a file from and then get the absolute path.
If you have a clean suggestion on how I can move from the file://// string to the / string I'm all ears.
Thanks
Converting from file uri to file path:
String fileUri = "file:///storage/emulated/0/";
String filePath = Uri.parse(fileUri).getPath();
Converting from file path to file uri:
String filePath2 = "/storage/emulated/0/";
String fileUri2 = Uri.fromFile(new File(filePath2)).toString();
I need to extract up to the directory a file is in, in the folder path. To do so, I created a simple regex. Here is an example path \\myvdi\Prod\2014\10\LTCG\LTCG_v2306_03_07_2014_1226.pfd
The regex below will find exactly what I need, but my problem is storing it into a variable. This is what I have below. It fails at the string array
String[] temp = targetFile.split("\\.*\\");
folder = temp[0];
Suggestions?
Thanks!
Edit
The exception being thrown is: java.util.regex.PatternSyntaxException: Unexpected internal error near index 4
If your path is valid within your file system, I would recommend not using regex and using a File object instead:
String path = "\\myvdi\\Prod\\2014\\10\\LTCG\\LTCG_v2306_03_07_2014_1226.pfd";
File file = new File(path);
System.out.println(file.getParent());
Output
\\myvdi\\Prod\\2014\\10\\LTCG\\
Simply, you need:
String path = "\\myvdi\\Prod\\2014\\10\\LTCG\\LTCG_v2306_03_07_2014_1226.pfd";
String dir = path.substring(0, path.lastIndexOf("\\") + 1);
You should use Pattern & Matchers which are much more powerful; from your description I'm not sure if you want to get the whole folder path, but if it is, here is a solution:
String s = "\\\\myvdi\\Prod\\2014\\10\\LTCG\\LTCG_v2306_03_07_2014_1226.pfd";
Pattern p = Pattern.compile("^(\\\\+[a-zA-Z0-9_-]+\\\\+([a-zA-Z0-9_-]+\\\\+)+).+$");
Matcher m = p.matcher(s);
if(m.matches()){
System.out.println(m.group(m.groupCount()-1));
}
I have DirectoryPath:
data/data/in.com.jotSmart/app_custom/folderName/FileName
which is stored as a String in ArrayList
Like
ArrayList<String> a;
a.add("data/data/in.com.jotSmart/app_custom/page01/Note01.png");
Now from this path I want to get page01 as a separate string and Note01 as a separate string and stored it into two string variables. I tried a lot, but I am not able to get the result. If anyone knows help me to solve this out.
f.getParent()
Returns the pathname string of this abstract pathname's parent, or null if this pathname does not name a parent directory.
For example
File f = new File("/home/jigar/Desktop/1.txt");
System.out.println(f.getParent());// /home/jigar/Desktop
System.out.println(f.getName()); //1.txt
Update: (based on update in question)
if data/data/in.com.jotSmart/app_custom/page01/Note01.png is valid representation of file in your file system then
for(String fileNameStr: filesList){
File file = new File(fileNameStr);
String dir = file.getParent().substring(file.getParent().lastIndexOf(File.separator) + 1);//page01
String fileName = f.getName();
if(fileName.indexOf(".")!=-1){
fileName = fileName.substring(0,fileName.lastIndexOf("."));
}
}
For folder name: file.getParentFile().getName().
For file name: file.getName().
create a file with this path...
then use these two methods to get directory name and file name.
file.getParent(); // dir name from starting till end like data/data....../page01
file.getName(); // file name like note01.png
if you need directory name as page01, you can get a substring of path u got from getparent.
How about using the .split ?
answer = str.split(delimiter);
url = new java.net.URL(s) doesn't work for me.
I have a string C:\apache-tomcat-6.0.29\webapps\XEPServlet\files\m1.fo and need to make a link and give it to my formatter for output, but malformed url recieved. It seems that it doesn't make my string to url.
I want also mention, that file m1.fo file is in files folder, in my webapp\product\, and I gave the full path to string like: getServletContext().getRealPath("files/m1.fo"). What I am doing wrong? How can I recieve the url link?
It is possible to get an URL from a file path with the java.io.File API :
String path = "C:\\apache-tomcat-6.0.29\\webapps\\XEPServlet\\files\\m1.fo";
File f = new File(path);
URL url = f.toURI().toURL();
Try: file:///C:/apache-tomcat-6.0.29/webapps/XEPServlet/files/m1.fo
It isn't preferable to write file:/// . Indeed it works on windows system,but in unix - there were problems.
Instead of using
myReq.put("xml", new String []{"file:" + System.getProperty("file.separator") +
getServletContext().getRealPath(DESTINATION_DIR_PATH) +
System.getProperty("file.separator") + xmlfile});
you can write
myReq.put("xml", new String [] {getUploadedFileURL (xmlfile)} );
, where
public String getUploadedFileURL(String filename) {
java.io.File filePath = new java.io.File(new
java.io.File(getServletContext().getRealPath(DESTINATION_DIR_PATH)),
filename);
return filePath.toURI().toURL().toString();
A file system path is not a URL. A URL is going to need a protocol prefix for one. To reference file system use "file:" in front of your path.