Android - Hide album art from image gallery activity - android

I've made an image gallery activity that is needed for my app. All works well except that it shows an image album for every music album I have in my SD card that contains album art. The default gallery app manages to hide these albums-pictures. How can I achieve the same ?
I get image albums with this code:
Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] projection = { MediaStore.Images.Media._ID, MediaStore.Images.Media.BUCKET_ID,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME,MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
ArrayList<String> ids = new ArrayList<String>();
if (cursor != null) {
while (cursor.moveToNext()) {
PhotoAlbum album = new PhotoAlbum();
int columnIndex = cursor.getColumnIndex(MediaStore.Images.Media.BUCKET_ID);
album.id = cursor.getString(columnIndex);
if (!ids.contains(album.id)) {
//get album name
columnIndex = cursor.getColumnIndex(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
album.name = cursor.getString(columnIndex);
//get image id for the album cover
columnIndex = cursor.getColumnIndex(MediaStore.Images.Media._ID);
album.coverID = cursor.getInt(columnIndex);
//get filename of album cover image
columnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
album.fileName=new File(cursor.getString(columnIndex)).getParentFile().getAbsolutePath();
album.count++; albumList.add(album); ids.add(album.id); album.bm=null;
} else { albumList.get(ids.indexOf(album.id)).count++; }
}
cursor.close();
}

Related

Querying for albums shows duplicates

I'm trying to list all the albums of songs available in my sd card. I did it successfully but I have a small problem. Example, if I an album "ABC", which contains 10 artists. Then, when I query for albums, it shows album "ABC" 10 times based on the artist. Album "ABC" will be displayed 10 times and each album "ABC" will contain songs of the particular artist.
It is displayed like this,
ABC\artistA
ABC\artistB........so on
What I want is it should display the album "ABC" once and when I open it all the songs of that album should be there.
Code:
final Uri uri = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;
final String _id = MediaStore.Audio.Albums._ID;
final String album_name = MediaStore.Audio.Albums.ALBUM;
final String totSongs = MediaStore.Audio.Albums.NUMBER_OF_SONGS;
final String artist_Name = MediaStore.Audio.Albums.ARTIST;
final String[] columns = { _id, album_name, totSongs,artist_Name };
Cursor cursor = getActivity().getContentResolver().query(uri, columns, null, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
id = cursor.getLong(cursor.getColumnIndex(_id));
String name = cursor.getString(cursor.getColumnIndex(album_name));
Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
albumArtUri = ContentUris.withAppendedId(sArtworkUri, id).toString();
totalSongs = cursor.getString(cursor.getColumnIndex(totSongs));
ArtistName = cursor.getString(cursor.getColumnIndex(artist_Name));
AlbumsModel s = new AlbumsModel(id, name, albumArtUri,totalSongs,ArtistName);
AlbumsList.add(s);
} while (cursor.moveToNext());
}
cursor.close();
Instead of using a List, you could use Map:
HashMap<String, AlbumModel> albums = new HashMap();
Cursor cursor = getActivity().getContentResolver().query(uri, columns, null, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
id = cursor.getLong(cursor.getColumnIndex(_id));
String name = cursor.getString(cursor.getColumnIndex(album_name));
Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
albumArtUri = ContentUris.withAppendedId(sArtworkUri, id).toString();
totalSongs = cursor.getString(cursor.getColumnIndex(totSongs));
ArtistName = cursor.getString(cursor.getColumnIndex(artist_Name));
AlbumsModel album = albums.get(id);
if(album == null){
album = new AlbumsModel(id, name, albumArtUri,totalSongs,ArtistName);
albums.put(id, album);
continue;
}
//update album music count
album.setTotalMusic(album.getTotalMusic() + totSongs);
albums.put(id, album);
} while (cursor.moveToNext());
}
cursor.close();
This is how you could display the Album/Artist with the Album only appearing once (this assumes that the cursor is sorted by Album) :-
String previous_album = "";
for (AlbumsModel am: AlbumsList) {
if(am.getAlbumName().equals(previous_album)) {
Log.d("ALBUMINFO","\t\t" + am.getArtistName());
} else {
Log.d("ALBUMINFO",am.getAlbumName() + "\t" + am.getArtistName());
}
previous_album = am.getAlbumName();
}
This would follow your code and write the output to the log (obviously you would tailor this to suit) e.g. :-
01-24 20:54:21.705 2125-2125/albums.so.so48422731albums D/ALBUMINFO: ABC ArtistA
01-24 20:54:21.705 2125-2125/albums.so.so48422731albums D/ALBUMINFO: ArtistB
01-24 20:54:21.705 2125-2125/albums.so.so48422731albums D/ALBUMINFO: ArtistC

get real path from android URI after selecting image from gallery

I am ( a beginner android) trying to get the real path (ex: image_name.jpg etc) of Image selected from the gallery by the user.
Here is the code snippet:
if (requestCode == SELECT_FILE) {
Uri selectedImage = data.getData();
Bitmap bitmapImage;
try {
bitmapImage =decodeBitmap(selectedImage );
photoImage = (ImageView) findViewById(R.id.photoImage);
photoImage.setImageBitmap(bitmapImage);
photoName = (TextView) findViewById(R.id.designPhoto);
File thePath = new File(getRealPathFromURI(selectedImage));
photoName.setText(getRealPathFromURI(selectedImage));
} catch (Exception e) {
e.printStackTrace();
}
}
private String getRealPathFromURI(Uri contentURI) {
String thePath = "no-path-found";
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(contentURI, filePathColumn, null, null, null);
if(cursor.moveToFirst()){
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
thePath = cursor.getString(columnIndex);
}
cursor.close();
return thePath;
}
but I am not getting anything in the photoName textView also tried lots of codes and guides but with no sucess.
but when I tried it with
photoName.setText(selectedImage.getPath());
I am getting
/document/image:n (where n=any numbers) ex:/document/image:147
but I want a proper file name or path ex: image_name.jpg or /path/image_name.png
trying it from last 3 hours, it would be great if anyone could help me out. humble Thanks in advance.
I got it working using
MediaStore.Images.Media.DISPLAY_NAME
updated and working code might help others:
private String getRealPathFromURI(Uri contentURI) {
String thePath = "no-path-found";
String[] filePathColumn = {MediaStore.Images.Media.DISPLAY_NAME};
Cursor cursor = getContentResolver().query(contentURI, filePathColumn, null, null, null);
if(cursor.moveToFirst()){
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
thePath = cursor.getString(columnIndex);
}
cursor.close();
return thePath;
}

How to show album art , song name ,duration, and the artist name in a ListView

hello i wanna make an android music app for that i want my listView which shows all the songs to display album art of that song , artist name , duration and song name
i have succeeded showing all the songs in the listview but unable to display album art etc
so can anyone help me in this??
Thanks in advance
You can use content provider for this.
Hope this code may help you to start up.
final Cursor mCursor = getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[] { MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.ALBUM_ID }, null, null,
"LOWER(" + MediaStore.Audio.Media.TITLE + ") ASC");
int count = mCursor.getCount();
String[] songs = new String[count];
if (mCursor.moveToFirst()) {
do {
String songname = mCursor
.getString(mCursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));
String sonpath = mCursor.getString(mCursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
String artistname = mCursor.getString(mCursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
String albumid = mCursor
.getString(mCursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
} while (mCursor.moveToNext());
Constant.sdCardmusic = allsongs;
}
mCursor.close();
}
If you want to get album picture then you can pass album id getting from above code into below method:
private Bitmap getArtistImage(String albumid) {
Bitmap artwork = null;
try {
Uri sArtworkUri = Uri
.parse("content://media/external/audio/albumart");
Uri uri = ContentUris.withAppendedId(sArtworkUri,
Long.valueOf(albumid));
ContentResolver res = mContext.getContentResolver();
InputStream in = res.openInputStream(uri);
artwork = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Exception", e.toString());
}
return artwork;
}
Google has a great example of a music player for phone , chromecast and AUTO on Github https://github.com/googlesamples/android-UniversalMusicPlayer

How to get Image Name in Android?

I have 2 Buttons in My activity. 1st One: Upload Image 2nd One: Get Uploaded image name. Now i have Pressed "Upload Image" Button and selected a image from gallery and that image is displayed in ImageView. Then i Pressed 2nd Button "Get Uploaded Image Name" and a Toast will display the name of that image which is in ImageView. I want the code of this only.
Uri uri = data.getData();
String[] projection = { MediaStore.Images.Media._ID, MediaStore.Images.Media.BUCKET_ID,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
ArrayList<String> ids = new ArrayList<String>();
mAlbumsList.clear();
if (cursor != null) {
while (cursor.moveToNext()) {
Album album = new Album();
int columnIndex = cursor.getColumnIndex(MediaStore.Images.Media.BUCKET_ID);
album.id = cursor.getString(columnIndex);
if (!ids.contains(album.id)) {
columnIndex = cursor.getColumnIndex(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
album.name = cursor.getString(columnIndex);
columnIndex = cursor.getColumnIndex(MediaStore.Images.Media._ID);
album.coverID = cursor.getLong(columnIndex);
mAlbumsList.add(album);
ids.add(album.id);
} else {
mAlbumsList.get(ids.indexOf(album.id)).count++;
}
}
cursor.close();
This is the way that you can retrieve the image name.. :
Uri selectedImage = data.getData();
String selectedImageName = selectedImage.getLastPathSegment();

fetching images from gallery on android phones with internal storage

Hi I am developing an Android Gallery app where I am fetching images from built in gallery and displaying it.I am using the code as below
String[] projection = {MediaStore.Images.Thumbnails._ID};
Cursor cursor = getContentResolver().query(MediaStore.Images.Thumbnails.INTERNAL_CONTENT_URI,
projection, // Which columns to return
null, // Return all rows
null,
null);
int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
int size = cursor.getCount();
// If size is 0, there are no images on the SD Card.
if (size == 0)
{
Log.e("size 0","0");
}
The Problem is when I run this code on Phones having only internal storage (Galaxy Nexus) I am getting Log which says size as Zero even though there are images in built in gallery. How do I resolve this.
Please Help.Thanks!
To get list of Gallery images you can try this
String[] projection = new String[]{
MediaStore.Images.Media._ID,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
MediaStore.Images.Media.DATE_TAKEN
};
Uri imageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
Cursor cur = getContentResolver().query(imageUri,
projection, // Which columns to return
null, // Which rows to return (all rows)
null, // Selection arguments (none)
null // Ordering
);
Log.i("Images Count"," count="+cur.getCount());
Try this on your button like "Browse"
browse.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
and you cal also set selected image into your ImageView as
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
picturePath = cursor.getString(columnIndex);
cursor.close();
imageView = (ImageView) findViewById(R.id.property_image);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
In first block of code i use startActivityForResult(i, RESULT_LOAD_IMAGE); this return result to called activity and we can get this result by second block of code, and set selected image in your ImageView
String[] projection = {MediaStore.Images.Media._ID};
Replace MediaStore.Images.Thumbnails.INTERNAL_CONTENT_URI with MediaStore.Images.MEDIA.EXTERNAL_CONTENT_URI
Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection, // Which columns to return
null, // Return all rows
null,
null);
int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
int size = cursor.getCount();
// If size is 0, there are no images on the SD Card.
if (size == 0)
{
Log.e("size 0","0");
}
EDIT: Get the list of thumbnails and get the image URI from the cursor
Uri uri=MediaStore.Images.Thumbnails.getContentUri("external");
Cursor cursor=MediaStore.Images.Thumbnails.queryMiniThumbnails
(getContentResolver(), uri, MediaStore.Images.Thumbnails.MINI_KIND,null);
if( cursor != null && cursor.getCount() > 0 ) {
String uri = cursor.getString( cursor.getColumnIndex( MediaStore.Images.Thumbnails.DATA ) );
}

Categories

Resources