I am creating a music player app and I am querying songs with the following code.
if(musicResolver != null){
musicCursor = musicResolver.query(musicUri,null,null,null,null);
}
if(musicCursor != null && musicCursor.moveToFirst()) {
int idCol = musicCursor.getColumnIndex(MediaStore.Audio.Media._ID);
int titleCol = musicCursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
int artistCol = musicCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
do {
long id = musicCursor.getLong(idCol);
String title = musicCursor.getString(titleCol);
String artist = musicCursor.getString(artistCol);
MusicService.songsSet.add(new Song(id,title,artist));
} while (musicCursor.moveToNext());
}
The problem is along with songs I am getting WhatsApp audio files. Is there a way to filter them.
There can be several ways by which you can filter out WhatsApp Audio Records. I am listing the most basic and easiest approach which you can use:
1. Ignoring the Audio folder of WhatsApp (WhatsApp Audio): Get the real path from the URI and ignore all files having path (WhatsApp/WhatsApp Audio/*).
2. Ignore Files with the names starting with AUD-000000-WA0000.*: This is again a pattern matching (Regex) to ignore all files having name matching the above pattern. WhatsApp Audio files have a specific file name pattern. So you can easily ignore them too.
Both these methods should get you to filter out WhatsApp Audio files and leave you with other sound files only.
You Can you below 2 lines, it is working for me, it is avoided to take Whatsapp Audio while querying to the content manager for the audio file.
where.append(" AND " + MediaStore.Audio.Media.TITLE + " NOT LIKE 'AUD%'");
where.append(" AND " + MediaStore.Audio.Media.TITLE + " NOT LIKE 'PTT%'");
Related
I'm building a Custom Control (a Gallery Picture Picker)
basically trying to re-make a this : Microsoft.Maui.Media.MediaPicker.PickPhotoAsync()
but i didn't find anyway to retrieve user pictures on MAUI ..
on Xamarin.Android i used this code :
var uriExternal = MediaStore.Images.Media.ExternalContentUri;
string[] projection = { (MediaStore.Images.Media.InterfaceConsts.Id),
(MediaStore.Images.Media.InterfaceConsts.DateAdded),
(MediaStore.Images.Media.InterfaceConsts.RelativePath) };
var cursor = Android.App.Application.Context.ContentResolver.Query(uriExternal, projection, null, null, MediaStore.Images.Media.InterfaceConsts.DateAdded);
if (cursor != null) {
int columnIndexID = cursor.GetColumnIndexOrThrow(MediaStore.Images.Media.InterfaceConsts.Id);
int RelativePathID = cursor.GetColumnIndexOrThrow(MediaStore.Images.Media.InterfaceConsts.RelativePath);
while (cursor.MoveToNext()) {
long imageId = cursor.GetLong(columnIndexID);
string rPath = cursor.GetString(RelativePathID);
//Get the Picture's URI
Android.Net.Uri uriImage = Android.Net.Uri.WithAppendedPath(uriExternal, "" + imageId);
...
}
}
with this Code, given the required permissions, i could get the external path of all pictures of an Android phone to then use them in my PicturePicking custom control ..
==> how can i do this in Microsoft MAUI !! (targeting Android & IOS)
(just to be clear, of course i know that i could just use MediaPicker's [PickPhotoAsync] function, but i clearly don't want that, it's not an option)
Thanks in advance for any help !
So after 2 days of searching.. i've came to the conclusion that the only way to do this is by using Plateform Specific Code ..
(i tryed it successfully)
I'm now trying to call the resources( in my application they are mp3 files and images).
For the mp3 file part, my code goes like this ( not working obviously!)
if(i==1)
{
hoho= MediaPlayer.create(getApplicationContext(), R.raw.univ_day1);
hoho.start();
}
else if (i==2)
{
hoho= MediaPlayer.create(getApplicationContext(), R.raw.univ_day2);
hoho.start();
}
...
In my application, there are hundreds of mp3 file inside the application. So what I want to do is briefly summarize code into something like this.
hoho=MediaPlayer.crate(getApplicationContet(), R.raw.univ_day+"i"); //This also looks really awkward.
If I knew how to write down code just like above one. How can I handle the name of raw files and the form like "R.raw...."? If I can do, then I also can apply similar approach to the image resources.
You can get resource id by its name using public int getIdentifier (String name, String defType, String defPackage) method.
For example:
int id = getResources().getIdentifier("univ_day"+i, "raw", getPackageName());
hoho = MediaPlayer.create(getApplicationContext(), id);
We can list all the names of raw assets, which are basically the filenames without the extensions, we can do this as -
public void listRaw(){
Field[] fields=R.raw.class.getFields();
for(int count=0; count < fields.length; count++){
Log.i("Raw Asset: ", fields[count].getName());
}
}
Next you'll need to refer to them by the integer assigned to that resource name. You can get this integer as:
int resourceID=fields[count].getInt(fields[count]);
This is the same int which you'd get by referring to R.raw.yourRawFileName.
In your code you can use this like -
hoho= MediaPlayer.create(getApplicationContext(), resourceID);
hoho.start();
Next you can apply JAVA logic to play previous current and next media files. Please let know if you need help here also.
i am trying to retrieve alarm information from content provider using following code
final String tag_alarm = "tag_alarm";
Uri uri = Uri.parse("content://com.android.deskclock/alarm")
Cursor c = getContentResolver().query(uri, null, null, null, null);
Log.i(tag_alarm, "no of records are" + c.getCount());
Log.i(tag_alarm, "no of columns are" + c.getColumnCount());
if (c != null) {
String names[] = c.getColumnNames();
for (String temp : names) {
System.out.println(temp);
}
if (c.moveToFirst()) {
do {
for (int j = 0; j < c.getColumnCount(); j++) {
Log.i(tag_alarm, c.getColumnName(j);
+ " which has value " + c.getString(j));
}
} while (c.moveToNext());
}
}
it is giving me error permission denial i copied this code from curious answer from query Get alarm infomation
in the Nguyen's comment he pointed a solution "If i embed this code in Android source code and run image file, it can pass "permission denied" error and retrieve alarm information. Anyway, thanks your tip :) " how to embed a code in android source code and run image file ?? please explain i always create a project in eclipse and then code and run it as run application.please explain this trick
In my opinion, because of each manufacturer had implemented their own Clock App,
So default AlarmClockApp of Android will be replaced depend on each manufacturer, that makes your code can not be run successful if Android Os had been modified by Manufacturers.
So I think we can not handle all the devices in this case, instead of that, we should handler it by manufacturer of devices.
With Samsung devices, it's ClockPackage and in androidManifest :
<provider
android:name=".alarm.AlarmProvider"
android:authorities="com.samsung.sec.android.clockpackage"
android:exported="true"
android:readPermission="com.sec.android.app.clockpackage.permission.READ_ALARM"
android:writePermission="com.sec.android.app.clockpackage.permission.WRITE_ALARM" >
</provider>
So we can read data of alarm in Samsung devices by :
add permission in manifest:
<uses-permission android:name="com.sec.android.app.clockpackage.permission.READ_ALARM" />
then get Uri by below :
Uri uri = Uri.parse("content://com.samsung.sec.android.clockpackage/alarm");
Use Uri :
Cursor c = getContentResolver().query(uri, null, null, null, null);
if (c == null) { // that mean devices is not belong to Samsung manufacturer,
// we should use an other uri (don't for get to add permission)
AlarmLog.w("Can not read cursor");
}
AlarmLog.i(tag_alarm, "no of records are " + c.getCount());
AlarmLog.i(tag_alarm, "no of columns are " + c.getColumnCount());
if (c != null) {
String names[] = c.getColumnNames();
for (String temp : names) {
AlarmLog.d(tag_alarm, temp);
}
if (c.moveToFirst()) {
do {
for (int j = 0; j < c.getColumnCount(); j++) {
AlarmLog.i(tag_alarm, c.getColumnName(j)
+ " which has value " + c.getString(j));
}
} while (c.moveToNext());
}
}
Hope it's helpful and receive code for others manufactures.
Look at the definition of the content provider in AndroidManifest.xml
<provider android:name="AlarmProvider"
android:authorities="com.android.deskclock"
android:exported="false" />
The exported is false, which means a 3rd-party app cannot access it. Permission denial as a result.
how to embed a code in android source code and run image file
It means you modify the Android source(Provided by google). I don't think it's useful in your case.
You can do that in a rooted device, by directly modify the contents in sqlite database. I don't think there is a solution to work on all existing Android platforms.
In general, sqlite database files are under /data/data/app-package-name/databases/database-name, so in this example, it should be /data/data/com.android.deskclock/databases/com.android.deskclock or something similar. You can pull the file out by adb pull and open it using SqliteExplorer to check whether it is what you want.
For how to modify this db file, check Using your own SQLite database in Android applications
As said there's no way to do this without root, but you can monitor when the next alarm is and when the value changes with the following value:
Settings.System.getUriFor(Settings.System.NEXT_ALARM_FORMATTED).toString()
This will give you a string with the next alarm.
I want to get all the browser history records from different browsers in android mobile phone ?(Maybe as you know, there are usually more than one browser apps in a phone ).Is there anyone has done this successfully and give me a hint ? Example code is preferred.Any help will be helpful.. Thanks a lot in advance ! :)
Just look at this. I used it in my code and I am getting browser history through it(Default Browser).
String[] proj = new String[] { Browser.BookmarkColumns.TITLE, Browser.BookmarkColumns.URL };
String selection = Browser.BookmarkColumns.BOOKMARK + " = 0"; // 0 = history, 1 = bookmark
mCursor = this.managedQuery(Browser.BOOKMARKS_URI, proj, selection, null, null);
this.startManagingCursor(mCursor);
mCursor.moveToFirst();
String title = "";
String url = "";
if (mCursor.moveToFirst() && mCursor.getCount() > 0) {
while (mCursor.isAfterLast() == false && cont) {
title = mCursor.getString(mCursor.getColumnIndex(Browser.BookmarkColumns.TITLE));
url = mCursor.getString(mCursor.getColumnIndex(Browser.BookmarkColumns.URL));
// Do something with title and url
mCursor.moveToNext();
}
}
Hope this helps you.
Limitations :
Browser.BOOKMARKS_URI will, at most, work for the open source Browser app that is part of the Android Open Source Project. Device manufacturers are welcome to replace that app with something else that will not be recording its history, bookmarks, or anything else in that ContentProvider. Similarly, users are allowed to download third-party browsers, which may not be storing things in that ContentProvider.
I am writing an android app which requires me to process a very large file(say 50MB). The file contains three entries-A userid, artistid and the number of times the user has listened to that artist. So now I write the code to find out who the most popular artist is based on the number of times the artist has been heard. I achieve this by hashmapping each of the artist id(key) and then the number of times he's been heard (value).
The code works fine as long as the file is below 20B (I run the app on Nexus S 4g, so the heap is 32MB), but I get an 'Out of memory' error for larger files. I realize the code is very badly written. Any suggestions as to how I could get through this problem!
while ((text = inRd.readLine()) != null) {
StringTokenizer st = new StringTokenizer(text);
while (st.hasMoreTokens()) {
int userid = Integer.parseInt(st.nextToken());
artistid = Integer.parseInt(st.nextToken());
int no_times = Integer.parseInt(st.nextToken());
if (hm.containsKey(artistid)) {
Integer oldval = (Integer) hm.get(artistid);
Integer newval = no_times + oldval;
hm.remove(artistid);
hm.put(artistid, newval);
} else
hm.put(artistid, no_times);
}
}
A hashmapping is not the most efficient way to store things. Try a Vector or straight arrays.
Hope this Helps
Cliff