Faster media scanning algorithm for android - android

I am working n in the meanwhile learning to code in android
I came across this mediastore which returns all the details about all the mp3 files stored on the card(Internal & External).
But this method is very very slow
I guess that's what has been implemented in the default music application
No wonder it sometimes fails to find out all the files...
I was thinking of implementing a faster search algorithm for this purpose, But am confused with the initial requirements of these algorithms
1: I thought of implementing the Binary search method (Divide n Conquer) to find files, but then the algorithm requires information about the number of files to be scanned.How do I get that?
2: I thought of implementing separate threads for each divided cluster.But then will it really work?
Plz help me in this!
the last question : How in the world does poweramp find out all the files so quickly,
My android has about 200 songs on the card, but this app only takes some seconds to get them all!!
Really puzzled!!

Try this code:
go to Download and DCIM folder there you can use this command to get all files and if you want to filter files then here is the link
Process process = null;
try {
process = Runtime.getRuntime().exec("ls -l");
} catch (IOException ez) {
ez.printStackTrace();
}
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder total = new StringBuilder();
String line;
try {
while ((line = in.readLine()) != null) {
System.out.println("line: "+line.toString());
total.append(line);
}
} catch (IOException ex) {
ex.printStackTrace();
}
System.out.println("Command Output: "+total.toString());

Related

Local Web Server with NanoHTTPD

I am trying to create a local web server using NanoHTTPD on Android. I know how to use it for a single file doing something like:
#Override
public Response serve(IHTTPSession session) {
String answer = "";
try {
FileReader index = new FileReader(fileLocation);
BufferedReader reader = new BufferedReader(index);
String line = "";
while ((line = reader.readLine()) != null) {
answer += line;
}
reader.close();
} catch(IOException ioe) {
Log.w("Httpd", ioe.toString());
}
return newFixedLengthResponse(answer);
}
and setting it to port 8080 so I can browse to http://localhost:8080 and view the file. However, I would like to use NanoHTTPD to put an entire directory on the server and not just a single file, in order to run an HTML5 game. Kinda similarly to how I can browse to a folder in Command Prompt on Windows and use
python -m http.server 4444
in order to browse through the folder as if it were a website at http://localhost:4444. I'm not sure how to go about it using NanoHTTPD. Can someone help me figure out where to start, or is there another library out there that's much easier to use?
Thanks in advance. Any help would be greatly appreciated.
You are on the right path.
Quick answer look here.
What you shod do is generate HTML files of directory structure to the user.
Meaning when the user is first contacting you read the local directory that you determine as root directory and create an HTML file as a response with the file names as links.
If the user then clicks on a single file serve it like you already do.
I found an answer demonstrating how to generate HTML files dynamically that you can use here.

Getting Shared Preferences while Migrating Project from Cocos2dX to Unity Android

I had to port my game from Cocos2dX to Unity for various reasons.
I have now ported the project successfully but to launch it I have to make a mechanism to get old user data and store it in new structure, like number of levels locked, high score of users etc.
While searching I came to know that COCOS2dX stored data on user's device on following path
system/data/data/mygamepackage/shared_prefs/Cocos2dxPrefsFile.xml
Is there anyway to get data from above mentioned path?
The above path should be accessible by the app it self.
I am not able to read that file I get following error.
ENOENT No Such file or Directory
The file is there I can see that on rooted device via Root Browser but I get the error when I run the app on same device.
All I need is to access the file programmatically and later I will parse it and will store it via Unity for future use.
Looking forward for a positive and quick response.
I dont know why I am unable to get the file through Unity Engine, I had to write custom plugin is Java and use that in Unity Project and that some how has worked.
Following is the code which return string after reading the file on both rooted and non rooted devices
public static String getCocos2DPrefsFile(String mPackageName)
{
//Get the text file
File file = new File("/data/data/"+mPackageName+"/shared_prefs/","Cocos2dxPrefsFile.xml");
//Read text from file
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
br.close();
}
catch (IOException e) {
//You'll need to add proper error handling here
Log.d("Error", e.toString());
}
return text.toString();
}

Android Read and Write File to Serial Port at Specific Frequency

Greetings. I have been using Serial PORT API to Transmit Data to Serial Port and the following code to read and write file to serial port.
File file = new File(musicpath + "file.txt");
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line="";
int c,counter=0;
while ((c = br.read()) != -1) {
String s = new StringBuilder().append("").append((char)c).toString();
MainActivity.sendDataChar(s);
}
}
catch (IOException e) {
//You'll need to add proper error handling here
}
I have been transmitting 8 BIT MONO WAVE FILES # 22Khz from SD CARD using RS485 and I do have hardware to playback the transmitted data using DAC. (BOTH TRANSMITTER and RECEIVER uses ARM7 - LPC2148).
Now my idea is to have the 8 BIT WAVE FILE stored in SDCARD of Android Device and transmit the same at specified frequency using timer.
I wanted to know how to do this. I tried the above method but I do understand that the transmission frequency does not match as I needed. (PORT OPENED AT 230400 baud rate).
With the above code, a 155 KB File should take approximately 6-7 seconds to finish up the transmission character by character. But it really takes about 15-20 seconds and I dont have the audio output at the receiver end. Just the noise.
Kindly advise me and give some ideas on how to do it in realtime just like the way I managed to do it in ARM7.
Thanks a Lot

read txt file in android from my mobile device

I have 4 txt files in my desktop.
I create an android app where I will read these files and print these in an EditText, but
these files are drug drop in ddms -> myapp -> data -> files.
So with emulator my app play correctly, but in my mobile phone, don't read these files and close the app.
This is my code:
public String ReadFileTrack1()
{
FileInputStream fIn = null;
InputStreamReader isr = null;
char[] inputBuffer = new char[900];
String data = null;
try
{
fIn = openFileInput(tracks[0]+".txt");
isr = new InputStreamReader(fIn);
isr.read(inputBuffer);
data = new String(inputBuffer);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
isr.close();
fIn.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
return data;
}
The code is correct, but don't play in my mobile phone.
What I can do for this problem?
How can i load these 4 txt files in my mobile phone so that my app play correctly?
You cannot write to an app's private storage directory on a secured device, unless the application first makes a subdirectory and grants others write access to it (which is generally a bad idea).
Put the files on the external storage (sdcard) instead, or package them as assets in the apk when you build it.
(There is an exception: if the application is built debuggable, then one can use the adb shell's run-as tool to copy something into the private directory as the appliction user, but that's obscure and not something you'd want to use as it does not apply to finalized apps. Generally that capability is used in the other direction, to examine what an app under development has done to its files or database when it is not working correctly)

Android app using android.permission.READ_LOGS - is that impolite?

I have an app that is available from the Android Market.
Some users have asked for a way of debugging when things don't work out as expected.
I have been looking into adding a menu item that will display the output of
Process mLogcatProc = null;
BufferedReader reader = null;
mLogcatProc = Runtime.getRuntime().exec(
new String[] {"logcat", "-d", "AndroidRuntime:E BDtN:V *:S" });
reader = new BufferedReader(new InputStreamReader (mLogcatProc.getInputStream()));
String line;
ArrayList listOfLogLines = new ArrayList();
while ((line = reader.readLine()) != null)
{
listOfLogLines.add(line);
}
Basically I am extracting the parts of the Log.* lines that my app has been writing and the errors that the AndroidRuntime has been throwing.
I have a working prototype that will display to the user the contents of the part of the log that I have extracted in a ListView.
I will have to add android.permission.READ_LOGS to the AndroidManifest.xml file in order for my app to have read access to the log, and this of course will be information that the user will be prompted with before installing.
And the question is if this is considered impolite, dangerous or otherwise out of the ordinary. Will it keep users from installing?
I wouldn't install an app that did this. If all you want is your own logs, your app can keep its own private log buffer that it writes into along with the system log.
You may not even need to do this though: http://android-developers.blogspot.com/2010/05/google-feedback-for-android.html
Don't make your live difficult and risk problems with your user! java.util.logging is available on android as well (and even forwarded to android.util.Log) and a java.util.logging.Handler will do everything you want.

Categories

Resources