String albumName = albumTitle;
String ExternalStorageDirectory = Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/My_Albums/" + albumName;
File targetDirectory = new File(ExternalStorageDirectory);
if (targetDirectory != null) {
File[] files = targetDirectory.listFiles();
if (files != null) {
filePathString = new String[files.length];
fileNameString = new String[files.length];
for (int i = 0; i < files.length; i++) {
filePathString[i] = files[i].getAbsolutePath();
System.out.println("filePAthStr: " + filePathString[i]);
fileNameString[i] = files[i].getName();
System.out.println("fileNameStr: " + fileNameString[i]);
customGridAdapter.add(filePathString[i]);
}
}
}
How do I get all folders with photo.
How do I get media from mediastore efficiently
Intially , album name will be empty
String albumName = albumTitle;
So if you aint enter the album name, the path would be like
String ExternalStorageDirectory = Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/My_Albums/";
it will go the parent path ,get all the albums under that node and display the number of images under it.
when once you provide albumname,it will get all images inside it and displays.In this scenario you need to loop inside album to cover each and every album under it and get the image paths from them.
UPDATE
For implementing this Logic refer link : Get All Folders With Photos
I got the solution, which was quite simple.
if(albumName != null && !albumName.equals("")){
// Only then set the adapter
}
Related
I have dynamic listView linked with JSONArray, in the adapter i'm trying to read the storage to check if the pdf file is downloaded already the Button text will be View and will call View method but if the the file not there the Button text will be Download and will call Download method.
I have been trying this code but it's not work
String path2 = Environment.getExternalStorageDirectory() + "/materialpdffolder/";
File path = new File(path2);
File list[] = path.listFiles();
for( int i=0; i < materialList.size() ; i++)
{
for( int x=0; x <= list.length ; x++) {
if (list[x].getName() != null) {
if (list[x].getName().equals(materialList.get(position).getMaterial_file_name().toString())) {
DVmaterial.setText("View(" + materialList.get(position).getMaterial_file_name().toString() +")" );
}}else
{
DVmaterial.setText("Download(" + materialList.get(position).getMaterial_file_name().toString() + ")");
}
}
}
The problem is when there’s no file, the application is stack and give error and not give any results for list[x].get Name() and when I use materiallist.size() for for looping it is doesn’tgive a correct answer
Try to change with this code
if (list[x].getName() != null) {
if (list[x].getName().equals(materialList.get(position).getMaterial_file_name().toString())) {
DVmaterial.setText("View(" + materialList.get(position).getMaterial_file_name().toString() +")" );
}else
{
DVmaterial.setText("Download(" + materialList.get(position).getMaterial_file_name().toString() + ")");
}
}
Remove one bracket from }}else and put it end of else
I am trying to fetch all songs from a specific folder. Currently, I am fetching all songs and then on basis of path I am looping and getting songs in the specific path.Any better way to do this?
for (int i = 0; i < totalSongList.size(); i++) {
String path = totalSongList.get(i).getPathId();
int index = path.lastIndexOf("/");
String folderPath1 = path.substring(0, index);
if (folderPath.equals(folderPath1))
songList.add(totalSongList.get(i));
}
I can't use below code also as it will fetch sub folder songs also.
musicResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null,MediaStore.Audio.Media.DATA + " like ? ",
new String[] {"%SPECIFIC_FOLDER_NAME%"}, null);
Here is java code without using mediaStore for retrieving all songs from the specific folder which may or maynot contain any subfolder.
public String File path = new File("your folder path here");
public void Search(File dir)
{
if(dir.isFile())
{
//get necessary songName over here
String songName = dir.getName();
//if you need path
String songPath = dir.getAbsolutePath();
//if your folder consists of other files other than audio you have to filer it
if(songName.endsWith(".mp3") || songName.endsWith(".MP3")
{
//this is the required mp3 files
//do what you want to do with it
}
}else if(dir.isDirectory())
{
File[] list = dir.listFiles();
for(int i=0;i<list.length();i++)
{
search(list[i]);
}
}
else
{
//handle exceptions here
}
}
how do I compare two arraylists?
I have a sdCoverList array and thumbnailList array.
02-04 11:05:05.210: I/System.out(4035): HASHED THUMBNAIL[-2122410790, 2043473787, 1914391068, 1785308349, 1656225630, 1527142911, 1398060192, 1268977473, 1139894754, 1010812035, 1242943301, 1113860582, 984777863, 855695144, 726612425, 597529706, 468446987, 339364268, 210281549]
02-04 11:05:05.210: I/System.out(4035): SD COVER LIST[-2122410790, 2043473787, 1914391068, 1785308349, 1268977473, 1656225630, 1527142911, 1139894754, 1398060192, 1010812035, 1242943301, 1113860582, 984777863, 855695144, 726612425, 597529706, 468446987, 339364268, 210281549, 717409028]
In my sd cover list, i have an extra value that i want to remove away but first i'd have to compare with thumbnailList array first. if both have the same elements, don't do anything but if theres an extra, remove it from sd card.
My array lists
for sdCoverList:
// CHECK DIRECTORY BEFORE DELETING THEM
File strPath = new File(Environment.getExternalStorageDirectory() + folderName+"/Covers");
File yourDir = new File(strPath, "");
if(yourDir.length()!=0)
for (File f : yourDir.listFiles()) {
if (f.isFile())
{
String name = f.getName();
sdCoverList.add(name);
//System.out.println(sdCoverList);
}
for hashedthumbnail
for (int a=0;a<=thumbnailList.size()-1;a++)
{
String hashed = String.valueOf(thumbnailList.get(a).hashCode());
Log.d("hashed",hashed);
hashedThumbnail.add(hashed);
}
List<String> sdCoverList = new ArrayList<String>();
sdCoverList.add("-2122410790");
sdCoverList.add("2043473787");
sdCoverList.add("717409028");
List<String> hashedThumbnail = new ArrayList<String>();
hashedThumbnail.add("-2122410790");
hashedThumbnail.add("2043473787");
System.out.println("sdCover List: " + sdCoverList);
System.out.println("hashedThumbnail List: " + hashedThumbnail);
List<String> temp = new ArrayList<String>();
temp.addAll(sdCoverList);
temp.removeAll(hashedThumbnail);
System.out.println("temp List: " + temp);
sdCoverList.removeAll(temp);
System.out.println("sdCover List: " + sdCoverList);
The output will be
sdCover List: [-2122410790, 2043473787, 717409028]
hashedThumbnail List: [-2122410790, 2043473787]
temp List: [717409028]
sdCover List: [-2122410790, 2043473787]
I would like to retrieve a ContentURI with appended id and be able to strip the id out.
In other words, I have a Content URI such as:
org.mycontentproviderAuthority/path1/path2/3
and I would like to get
org.mycontentproviderAuthority/path1/path2/
Is it possible to do that preferably with Uri methods? I mean I guess I could use a String tokenizer to remove the last digit, but Id assume that a uri api for that would be safer.
I tried uri.getAuthority() + uri.getPath() but that gives me back the whole ContentUri.
You might use the following:
static public Uri getUriWithoutLastNPathParts(final Uri uri, final int n) {
final List<String> segments = uri.getPathSegments();
final String[] newSegments = new String[segments.size() - n];
final Uri.Builder builder = new Uri.Builder();
builder.encodedAuthority(uri.getAuthority());
builder.encodedPath(uri.getPath());
builder.encodedQuery(uri.getQuery());
// no "appendPath", it messes up the thing
for (int i = 0; i < newSegments.length; i++) {
newSegments[i] = segments.get(i);
}
builder.encodedPath(TextUtils.join("/", newSegments));
return builder.build();
}
Usage:
Uri uri = Uri.parse("org.mycontentproviderAuthority/path1/path2/3");
Log.i("Segments", uri + " " + getUriWithoutLastNPathParts(uri, 1));
uri = Uri.parse("org.mycontentproviderAuthority/path1/path2/3?foo=bar");
Log.i("Segments", uri + " " + getUriWithoutLastNPathParts(uri, 2));
Here is an example to parse the ID from a Content URI
http://appulearn.blogspot.com/2012/05/getting-android-id-from-uri-content.html
how can i display all the files (.pdf, .doc, .ppt, .mp4, etc...) inside a folder in sdcard? i want to display their filenames.
here's what i've got:
File files = new File(Environment.getExternalStorageDirectory() + "/Downloads/Files/");
File[] list = files.listFiles();
int lists = files.listFiles().length;
String qwe = list.toString();
Log.d(LOG_TAG, "list: " + lists);
Log.d(LOG_TAG, "names: " + qwe + "\n"); //i want to show their filenames
for(int i=0; i < questionfiles.listFiles().length; i++){
if(list[i].isHidden()){
Log.d(LOG_TAG, "hidden path files.."+list[i].getAbsolutePath());
}
}
You have to use a ListActivity with an Adapter (ArrayAdapter for istance).
see this as example
EDIT: You can print the array content in this way:
Log.i(LOG_TAG, Arrays.toString(list));