Reputation: 277
I want to use the uri value of an image (location on disk) in my app. For that, I have to use a string that starts with /sdcard/...
I can open up a file manager to help select the picture, but even so every file manager adds something to the front, be it /mnt/sdcard or something else. I can't use substring because the number of letters in front of /sdcard isn't always the same.
I want to read this string from /sdcard onward, is there a way I can do this?
Upvotes: 0
Views: 371
Reputation: 765
U can use the Function
indexOf(String str)
where str is '/sdcard/'
this will give you the index of first occurrence of your string.
Upvotes: 0
Reputation: 3534
You can use String.lastIndexOf to find the last occurance of "/sdcard" and then read the rest using usual substring from index+offset ...
Upvotes: 0
Reputation: 827
Hmmm. Seems like a pretty simple question, use indexOf("/sdcard") to find the position of "/sdcard", then initiate your substring from that point.
String#substring( String#indexOf("/sdcard"), String#length() )
Upvotes: 0
Reputation: 2713
You can do this like so:
File cacheDir;
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
cacheDir = new File(
android.os.Environment.getExternalStorageDirectory(),
getString(R.string.cache_directory_image_cache));
} else {
cacheDir = getCacheDir();
}
String filepath = cacheDir.getAbsolutePath();
Then you have the path in filepath!
Upvotes: 1