MediaStore query don't get all artists and camera videos - java

I use the code bellow to query all artists of music files, the method works good but the problem it's missing artist column called and that column is visible in other music apps, so am I missing something in the query process ?
void loadAudioData() {
ContentResolver contentResolver = Objects.requireNonNull(getActivity()).getContentResolver();
final Uri uri = MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI;
String[] projection = {MediaStore.Audio.Artists._ID, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Artists.ARTIST,
MediaStore.Audio.Artists.NUMBER_OF_TRACKS};
cursor = contentResolver.query(uri, projection, null, null, null);
if (cursor != null && cursor.moveToFirst())
while (cursor.moveToNext())
songsList.add(new Song(cursor.getString(2), cursor.getString(0), cursor.getString(3)));
cursor.close();
String sortType = sharedPreferences.getString("artist_sort", "artist a to z");
boolean isReverse = sharedPreferences.getBoolean("artist_sort_isReverse", false);
if (sortType != null)
Song.sortArtist(songsList, sortType, isReverse);
}
the same problem here too but with camera videos, the query process gets all videos except camera videos.
public List<ImageVideo> getAllVideos(){
Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
String[] projection = {MediaStore.Video.Media.DATA, MediaStore.Video.Media.TITLE};
final String orderBy = MediaStore.Video.Media.DATE_TAKEN;
Cursor cursor = Objects.requireNonNull(getContext()).getContentResolver().query(uri, projection,
null, null, orderBy + " DESC");
videoList = new ArrayList<>();
if(cursor.moveToFirst()) {
while (cursor.moveToNext()) {
videoList.add(new ImageVideo(cursor.getString(1), cursor.getString(0)));
}
}
cursor.close();
return videoList;
}

Related

MediaStore Uri for External?

As a MediaStore newbie's, I dunno what's wrong with this uri:
MediaStore.Files.getContentUri("external")
Those uri's keeps returns to zero.
My example codes:
String file = null;
String sortBy = "";
if (filterSort == "time") {
sortBy = MediaStore.Files.FileColumns.DATE_MODIFIED;
} else if (filterSort == "size") {
sortBy = MediaStore.Files.FileColumns.SIZE;
}
String[] projection = {MediaStore.Files.FileColumns.DATA};
android.database.Cursor cursor = getContentResolver().query(MediaStore.Files.getContentUri("external"), projection, null, null, sortBy + "DESC");
if (cursor != null) {
while (cursor.moveToNext()) {
file = cursor.getString(0);
states.add(file);
}
cursor.close();
}
Trying to solve with
Uri.parse worked but did not apply sorting filter's. Back with
MediaStore.Files.getContentUri("external") , it keeps giving me this via Exception on Android 8.1 devices:
android.database.sqlite.SQLiteException:
no such column: date_modifiedDESC (code 1): , while compiling: SELECT _data FROM files WHERE ( invalid=0) ORDER BY date_modifiedDESC
Is there any way to solve/change/do?
Nor sure what blackapps is referring to as he does not address the question in any way.
There are a number of uris, depending what you want to extract.
for example:
public Uri get_audio_media_uri(){
Uri uri_to_use = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
uri_to_use = MediaStore.Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL);
} else {
uri_to_use = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
return uri_to_use;
}
or for artists;
public Uri get_audio_artist_uri(){
Uri uri_to_use = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
uri_to_use = MediaStore.Audio.Artists.getContentUri(MediaStore.VOLUME_EXTERNAL);
} else {
uri_to_use = MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI;
}
return uri_to_use;
}
the part "date_modifiedDESC" is what causes the issue as it requires a space "date_modified DESC"
I suggest you amend your projection, for example
String projection[];
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.Q) {
//no _DATA column in Q
projection = new String[]{
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.DATE_MODIFIED
};
} else {
projection = new String[]{
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.DATA
MediaStore.Audio.Media.DATE_MODIFIED
};
}
instead of MediaStore.Files.FileColumns.DATE_MODIFIED;

(loading images from gallery to app) emulator API 29 shows all pictures but when i run the mobile phone as an emulator no pictures are shown

public class ImagesGallery {
#SuppressLint("Recycle")
#RequiresApi(api = Build.VERSION_CODES.Q)
public static ArrayList<String> listOfImages (Context context){
Uri uri;
Cursor cursor;
int column_index_data;
ArrayList<String> listOfAllImages = new ArrayList<>();
String absolutePathOfImages;
uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] projection = new String[]{MediaStore.MediaColumns.DATA};
String orderBy = MediaStore.Video.Media.DATE_TAKEN;
cursor = context.getContentResolver().query(uri, projection, null, null, orderBy+" DESC");
if (cursor != null){
column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
while (cursor.moveToNext()){
absolutePathOfImages = cursor.getString(column_index_data);
listOfAllImages.add(absolutePathOfImages);
}
}
return listOfAllImages;
}
}
Activity mList (String-ArrayList) in Activity
private void loadImages() {
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new GridLayoutManager(this, 4));
mList = ImagesGallery.listOfImages(this);
galleryAdapter = new GalleryAdapter(PhotoActivity.this, mList, new GalleryAdapter.PhotoListener() {
#Override
public void onPhotoClick(String path) {
Glide.with(PhotoActivity.this).load(path).into(image);
imageData = Uri.fromFile(new File(path));
}
});
recyclerView.setAdapter(galleryAdapter);
gallery_number.setText("Photos ("+mList.size()+")");
}
How can i make (MediaStore.MediaColumns.DATA) situation compatible with android 10 I read the documentation but could not solve the problem,Any explanation about this situation would help me

Android (Java) - lookup contact by email and preview if exists

Right now we have an app where we (after permissions) lookup a contact, and if they exist, display their contact card. However the lookup seems very inefficient, it loops through everyone, doesn't seem right. Is there a better query contacts db to lookup ONLY contacts that have an email address that we need?
Current Code Below (not efficient):
public static void showCardIfPossible(String email, Context context) {
Long contactId = lookupByEmail(email, context);
if (contactId != null) {
viewCardFor(context, contactId);
}
}
public static Long lookupByEmail(String email, Context context) {
Long contactId = null;
ContentResolver cr = context.getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cursor == null || cursor.getCount() == 0) {
return null;
}
while (cursor.moveToNext())
{
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
Cursor cur1 = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
if (cur1 != null && cur1.getCount() > 0) {
while (cur1.moveToNext()) {
//to get the contact names
String contactName=cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String contactEmail = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
if(email.equals(contactEmail) || email.equals(contactName)){
contactId = Long.valueOf(id);
}
}
cur1.close();
}
}
cursor.close();
return contactId;
}
public static void viewCardFor(Context context, long contactId) {
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(contactId));
intent.setData(uri);
context.startActivity(intent);
}
Yes, you can just query directly on the Email.CONTENT_URI and using selection to find the right contact:
public static Long lookupByEmail(String email, Context context) {
ContentResolver cr = context.getContentResolver();
Long contactId = null;
String[] projection = new String[] { Email.CONTACT_ID };
String selection = Email.ADDRESS + "='" + email + "'";
Cursor cur = cr.query(Email.CONTENT_URI, projection, selection, null, null);
if (cur != null) {
if (cur.moveToFirst()) {
contactId = cur.getLong(0);
}
cur.close();
}
return contactId;
}

get location of cafe from sqlite and provide it as string in textview while Cafe name "=?" android Eclipse

here is my database helper (i need to select the KEY_LOCATION while KEY_NAME = ? )
public nranges getCafego(String cafesname) {
DataAccessClass dataAcessClass = new DataAccessClass(con);
SQLiteDatabase db = dataAcessClass.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, new String[] { KEY_ID,
KEY_NAME, KEY_LOCATION },
KEY_NAME + "=?", new String[] { String.valueOf(cafesname) }, null, null,
null, null);
if (cursor != null)
cursor.moveToFirst();
nranges rng = new nranges();
rng.setId(Integer.parseInt(cursor.getString(cursor
.getColumnIndex(KEY_ID))));
rng.setName(cursor.getString(cursor
.getColumnIndex(KEY_NAME)));
rng.setLocation(cursor.getString(cursor
.getColumnIndex(KEY_LOCATION)));
// return Note
return rng;
}
and here is my activity which i need to get the KEY_LOCATION as string and provide it in a textview
Intent i=getIntent();
String MyCafeName =i.getExtras().getString("MyCafeName");
txtcafename.setText(MyCafeName);
nrangehelper bud3=new nrangehelper(cafelistselected.this); // my database helper which name is (nrangehelper)
bud3.getCafego(MyCafeName); //Mycafename is the name of cafe which i need to select to get its location
// what to do next?????

Loading contact photos into contact list: random contact photos appear in wrong places

I have loaded my contacts into a list using extended SimpleCursorAdapter and now I'm trying to load the contact photos. When running the code random contact photos appear next to random contacts, even for those who don't have photos. Why doesn't it just get the photos for those contacts who have them and show next to them?
Here is the code:
public void bindView(View view, Context context, Cursor cursor) {
ImageView photo = (ImageView) findViewById(R.id.photo);
long photoId = cursor.getLong(cursor.getColumnIndex(ContactsContract.Contacts.PHOTO_ID));
Bitmap photoBitmap = loadContactPhoto(photoId);
if (photoBitmap != null) {
photo.setImageBitmap(photoBitmap);
}
And the code to loadContactPhoto:
public Bitmap loadContactPhoto(long id) {
Uri contactUri = ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, id);
byte[] data = null;
Cursor cursor = managedQuery(
contactUri, // Uri
new String[] { ContactsContract.CommonDataKinds.Photo.PHOTO }, // projection, the contact photo
ContactsContract.Contacts.PHOTO_ID + "!= 0", // where statement, only if the contact has a photo
null, null);
Log.i(LOG_TAG, "cursorCount: " + cursor.getCount()); // returns 1
if (cursor == null || !cursor.moveToNext()) {
return null;
}
data = cursor.getBlob(0);
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
return bitmap;
}
In bindView(), you're calling:
ImageView photo = (ImageView) findViewById(R.id.photo);
Shouldn't you be calling findViewById() on the view parameter?
ImageView photo = (ImageView) view.findViewById(R.id.photo);

Categories