comparing between two arraylists and remove extra element from there - android

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]

Related

Get all folders with photo - Android

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
}

Fetch all songs from specific path

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
}
}

get only last array vlaue from ArrayList android

I am using GridView with camera to show images (maximum 4 images)
i need all the captured images path in ArrayList ,
The problem is when the loop iterates , it give me previous array value along with latest array
Eg:
iteration 1 : [imagepath1]
iteration 2 : [imagepath1,imagepath2]
iteration 3 : [imagepath1,imagepath2,imagepath3]
I need only the latest iterated value
i.e : [imagepath1,imagepath2,imagepath3]
// ArrayList<String> imageList;
private List<String> RetriveCapturedImagePath() {
List<String> tFileList = new ArrayList<String>();
File f = new File(GridViewDemo_ImagePath);
if (f.exists()) {
File[] files = f.listFiles();
Arrays.sort(files);
//Log.e("f.listFiles();", "files "+files);
for (int i = 0; i < files.length; i++) {
File file = files[i];
if (file.isDirectory())
continue;
tFileList.add(file.getPath());
Log.e("file.getPath()", "karfile " + file.getPath());
imageList.add(file.getPath());
Log.e("imageList RetriveCapturedImagePath", "karimageList" + imageList);
}
}
return tFileList;
}

How can I insert data from a text file into a sql database in android?

I'm quite new to SQLite databases and need help using an external text file to create an initial database to store a bunch of items that can be selected and added to a second database. To put this all in context, I want to have an initial database that stores a list of grocery store items. Each item will have two traits with them, a NAME and a TYPE. So there are items such as( NAME Bread, TYPE Bakery. NAME Grape Juice, TYPE Beverage. NAME Toothbrush, TYPE Toiletries. )
I need to go from something like this:
GROCERYSTORE.TXT
-- -- -- -- -- --
NAME - TYPE
Bread - Bakery
Rolls - Bakery
Juice - Beverages
Soda - Beverages
to this:
dBHelper.insert("Bread", "Bakery");
or:
dBHelper.insert(name, type);
for each item in the text file.
First of all, I would like to know how to format this text file, or any other type of file that could be read and inserted into a database if a text file is not the easiest way to go about this.
Second, how would I go about reading this text file and inserting it into the database?
I'm thinking I would use a buffered reader and inputstream within a for loop to go and insert each item into the database. I'm just not sure how to parse each line to give each item a name and a type to be inserted into the database, and how to format a text file to be read.
Any help would be greatly appreciated as I have found very little about using external files to import data into an sql database that pertains to my situation.
In my apps that have a static or default database I use an xml file for my data sort of like this:
default_grocerystore.xml stored in assets folder:
<default_grocerystore>
<grocery>
<name>Bread</name>
<type>Bakery</type>
</grocery>
<grocery>
<name>Rolls</name>
<type>Bakery</type>
</grocery>
<grocery>
<name>Juice</name>
<type>Beverages</type>
</grocery>
<grocery>
<name>Soda</name>
<type>Beverages</type>
</grocery>
</default_grocerystore>
Then in the onCreate in my database class I create my database tables I populate them with the xml like this:
private void populateGrocery(SQLiteDatabase db) {
ArrayList<GroceryObj> groceryArrayList;
groceryArrayList = buildGroceryArrayList();
String insertStmt = null;
for (int i = 0; i < groceryArrayList.size(); i++) {
insertStmt = "INSERT INTO " + GROCERY_TABLE + " ("
+ GROCERY_KEY_NAME + ", " + GROCERY_KEY_TYPE + ") "
+ "VALUES (\""
+ groceryArrayList.get(i).getName()
+ "\", \""+groceryArrayList.get(i).getType()+"\");";
db.execSQL(insertStmt);
}
private ArrayList<GroceryObj> buildGroceryArrayList() {
ArrayList<GroceryObj> aL = new ArrayList<GroceryObj>();
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
InputStream raw = context.getAssets().open(
"default_grocerys.xml");
Document dom = builder.parse(raw);
Element root = dom.getDocumentElement();
NodeList groceryItems = root.getElementsByTagName("grocery");
for (int i = 0; i < groceryItems.getLength(); i++) {
String name= null;
String type= null;
Node item = groceryItems.item(i);
NodeList groceryItem = item.getChildNodes();
for (int j = 0; j < groceryItem.getLength(); j++) {
Node nodeItem= groceryItem.item(j);
String nodeName= noteItem.getNodeName();
if (nodeName.equalsIgnoreCase("name")) {
name= nodeItem.getFirstChild().getNodeValue();
} else if (nodeName.equalsIgnoreCase("type")) {
type= nodeItem.getFirstChild().getNodeValue();
}
}
aL.add(new GroceryObj(name, type));
}
} catch (Exception e) {
e.printStackTrace();
}
return aL;
}
}

display all files from specific folder from sdcard Android

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));

Categories

Resources