android


Content Resolver query returns empty cursor only on a Samsung Galaxy S4 running Lollipop


I have this code and I've tested it on a few devices, works on them, except for a Samsung Galaxy S4 running Lollipop. The getContentResolver().query() method returns an empty cursor, every time. If the if/else statement goes to the else, because of that, and tries insert I get another null as the result of the insert
String mediaTitle = String.format("My Artist%s", title);
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, mediaTitle);
values.put(MediaStore.MediaColumns.SIZE, k.length());
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");
values.put(MediaStore.Audio.Media.ARTIST, "My Artist");
values.put(MediaStore.Audio.Media.IS_RINGTONE, false);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
values.put(MediaStore.Audio.Media.IS_ALARM, false);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);
Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
String selection = String.format("%s = ?", MediaStore.MediaColumns.TITLE);
c = getContentResolver().query(uri, null, selection, new String[]{mediaTitle}, null); // RETURNS NULL only on Samsung Galaxy S4 with Lollipop
if (c.moveToFirst()) {
return uri.buildUpon().appendPath(String.valueOf(c.getInt(c.getColumnIndex(MediaStore.MediaColumns._ID)))).build();
} else {
return getContentResolver().insert(uri, values);
}
Turns out the insert method won't insert the ringtone if it isn't unique and I wasn't able to query for that Uri properly so I was trying to insert something that was already there.
I should have either fixed the way I query for it, which is strange, because on other devices it worked, or do this:
getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() + "\"", null);
before the query, which deletes the files that are already there, and allowed me to insert them anew and now the insert doesn't return null anymore. A bit of a brute force solution, but it helped me.

Related Links

Android unit test using ant with library project
Orientation (at configChanges) change makes my buttons stop work after orientation
buttons not working in android emulator with phonegap
How to launch the Android Download Manager
Bit depth issues on bitmaps and screens (white is not white)
Combo preference in Android
Android Stack Trace - Where is error shown?
Unregister BroadcastReceiver in IntentService
Android rotate bitmap 90 degrees results in squashed image. Need a true rotate between portrait and landscape
Why i am not able to load Advertisement sometimes?
Android: How to add a vertical progress bar to a home screen widget?
Android: Live Video Streaming
connect micromax A70 android phone to eclipse
Android OpenGL lag every x frame
Best way to debug exception
ClientProtocolException thrown when using ThreadSafeClientConnManager to execute a http post

Categories

HOME
algorithm
embedded
design-patterns
time-complexity
webdriver
angular-formly
powerpoint
fftw
package
graphics
error-handling
knitr
permissions
vxml
retrofit2
sendgrid-api-v3
ibeacon-android
stellar.js
selenium-ide
code-generation
powershell-v5.0
apache2.4
lilypond
displaytag
comparator
entity-relationship-model
genexus-sd
cppreference
user-defined-functions
corenlp-server
anki
left-join
deltaspike
playstation-portable
installscript
engine.io
openalpr
gdata
urlsession
levenshtein-distance
parser-generator
subclipse
phpdbg
servermanager
fwrite
configurationmanager
launchctl
carrot2
spring-data-couchbase
colorbar
git-flow
arules
otp
quicktime
google-photos
rmongo
change-tracking
grails-plugin
expectations
android-wake-lock
blitline
mobiledoc-kit
jbase
relativelayout
morton-number
text-search
qtwebengine
packet-sniffers
android-cookiemanager
deform
post-commit-hook
cocos3d
insight.database
clickbank
http-patch
mongohq
neos-server
actiondispatch
asplinkbutton
android-sdk-2.3
frameworkelementfactory
jquery-blockui
boost-signals
infobright
jquery-address
procedures
denied
quotation-marks
http-daemon
presentation-layer
load-time
ie-compatibility-mode
delimited-text
lxr
entitykey
online-storage
mathematical-typesetting
cardspace

Resources

Encrypt Message