I have an android imageView.
I have set an OnClickListener to pick an image from Photo Gallery to be its src.
I now want to change the OnClick open this specific image in the photo gallery.
how do I do this? how to open a specific image in the photo gallery ?
You can query with particular photoID, from below code you got all image and it's id and from id you can find particular image.
final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
final String orderBy = MediaStore.Images.Media._ID;
imagecursor = getActivity().managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, null, orderBy);
int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);
this.count = imagecursor.getCount();
this.arrPath = new String[this.count];
ids = new int[count];
for (int i = 0; i < this.count; i++) {
imagecursor.moveToPosition(i);
ids[i] = imagecursor.getInt(image_column_index);
int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
arrPath[i] = imagecursor.getString(dataColumnIndex);
}
find adapter code and set it in gridview from
get full photogallery code
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 months ago.
Improve this question
My audio player on phone is showing "SQLite exception" and crashed. It needs external storage permission but I don't have a memory card. How to resolve this error?
Exception:
android.database.sqlite.SQLiteException: no such column: date_addedDESC (Sqlite code 1 SQLITE_ERROR): , while compiling: SELECT _id, _display_name, duration, _size, album_id FROM audio WHERE ((is_pending=0) AND (is_trashed=0) AND (volume_name IN ( 'external_primary' ))) AND (date_addedDESC), (OS error - 2:No such file or directory)
Main activity :
private void fetchPlayer() {
//define a list to carry players
List<player> mPlayer = new ArrayList<> ();
Uri mediaStoreUri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
mediaStoreUri = MediaStore.Audio.Media.getContentUri (MediaStore.VOLUME_EXTERNAL);
}else{
mediaStoreUri = MediaStore.Audio.Media. EXTERNAL_CONTENT_URI;
}
// define projection
String[] projection = new String[]{
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.SIZE,
MediaStore.Audio.Media.ALBUM_ID,
};
// order
String sortOrder = MediaStore.Audio.Media.DATE_ADDED + " DESC ";
// get the players
try (Cursor cursor = getContentResolver ().query (mediaStoreUri,projection,sortOrder,null,null)) {
// cache cursor indices
int idColumn = cursor.getColumnIndexOrThrow (MediaStore.Audio.Media._ID);
int nameColumn = cursor.getColumnIndexOrThrow (MediaStore.Audio.Media.DISPLAY_NAME);
int durationColumn = cursor.getColumnIndexOrThrow (MediaStore.Audio.Media.DURATION);
int sizeColumn = cursor.getColumnIndexOrThrow (MediaStore.Audio.Media.SIZE);
int albumColumn = cursor.getColumnIndexOrThrow (MediaStore.Audio.Media.ALBUM_ID);
//clear the previous loaded before adding loading again
while (cursor.moveToNext ()){
//get the values of a column for a given audio file
long id = cursor.getLong(idColumn );
String name = cursor.getString (nameColumn);
int duration = cursor.getInt (durationColumn);
int size = cursor.getInt (sizeColumn);
long albumId = cursor.getLong (albumColumn);
// player Uri
Uri uri = ContentUris.withAppendedId (MediaStore.Audio.Media.EXTERNAL_CONTENT_URI , id );
// album artwork uri
Uri albumArtWorkUri = ContentUris.withAppendedId (Uri.parse("content:// media/external/audio/albumart") , albumId);
// remove mp3 extension from players name
name = name.substring (0 , name.lastIndexOf ("."));
//player item
player player = new player (name , uri , albumArtWorkUri , size , duration , id );
//add player item to play list
mPlayer.add(player);
}
//display player
showPlayers(mPlayer);
}
}
private void showPlayers(List<player> mPlayer){
if(mPlayer.size () == 0){
Toast.makeText (this , "No Players" , Toast.LENGTH_SHORT ).show ();
return;
}
// save players
allPlayer.clear ();
allPlayer.addAll (mPlayer);
//update the tools bar title
String title = getResources ().getString (R.string.app_name) + "." + mPlayer.size ();
Objects.requireNonNull (getSupportActionBar ()).setTitle (title);
//layout manager
LinearLayoutManager layoutManager = new LinearLayoutManager (this);
recyclerView.setLayoutManager (layoutManager);
//players adapter
playerAdapter = new playerAdapter (this , mPlayer);
//set the adapter to recycleView
recyclerView.setAdapter (playerAdapter);
}
}
SortOrder is last parameter:
Cursor cursor = getContentResolver ().query (mediaStoreUri,projection,null,null,sortOrder)
ok I solve it's problem I just space in sortOrder variable and I just click:
1- Build
2- then Clean Project
3- and Rebuild Project
String sortOrder = MediaStore.Audio.Media.DATE_ADDED + " DESC ";
maybe to help any Developer
I'm developing camera app now and can record videos in app folder successfully.
Btw when I read videos from app folder with Cursor, it doesn't refresh cursor. (It means when I record video with app and whenever I want to view videos via app, it shows old videos. even when I close app and re-open app it shows old videos. )
Here's source code.
public static List<MyVideo> getAppVideos(Context context) {
final String[] projection = { MediaStore.Video.Media.DATA, MediaStore.Video.Media.DATE_TAKEN, MediaStore.Video.Media.DURATION};
final String selection = MediaStore.Images.Media.BUCKET_ID + " = ?";
final String[] selectionArgs = { APP_VIDEO_BUCKET_ID };
final Cursor cursor = context.getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
projection,
selection,
selectionArgs,
null);
ArrayList<MyVideo> result = new ArrayList<MyVideo>(cursor.getCount());
if (cursor.moveToFirst()) {
final int dataColumn = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
final int dateColumn = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATE_TAKEN);
final int durationColumn = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION);
do {
final String data = cursor.getString(dataColumn);
final String createdDate = cursor.getString(dateColumn);
final String durationTime = cursor.getString(durationColumn);
result.add(new MyVideo(data, DateUtils.convertToSeconds(durationTime), "^ " + DateUtils.getDateFromMilliSeconds(Long.parseLong(createdDate), "MMM dd, yyyy")));
} while (cursor.moveToNext());
}
cursor.close();
return result;
}
Could anyone please suggest solution?
If CursorDemo extends CursorAdapter, then you have to use adapter.swapCursor(cursor_update);
That should swap the old cursor out for the new one and reload the data. With swapCursor,since the old cursor is not closed.
I'm trying to grab album information from MediaStore. I can get the name and artist very easily but for some reason the ALBUM_ID column is giving me an error when I try to access it.
Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
at android.database.CursorWindow.nativeGetString(Native Method)
at android.database.CursorWindow.getString(CursorWindow.java:450)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51 )
at android.database.CursorWrapper.getString(CursorWrapper.java:114)
at sage.musicplayer.MainActivity.getAlbumList(MainActivity.java:1540)
at sage.musicplayer.MainActivity.onCreate(MainActivity.java:238)
I can't seem to find a solution. Any help is appreciated! Below is the method I have to grab the album information and add them to an ArrayList.
public ArrayList<Album> getAlbumList() {
ArrayList<Album> temp = new ArrayList<>();
/*String[] proj = {MediaStore.Audio.Albums.ALBUM_ID,
MediaStore.Audio.Albums.ARTIST,
MediaStore.Audio.Albums.ALBUM,
MediaStore.Audio.Albums.NUMBER_OF_SONGS
};*/
Cursor albumCursor = getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, null, null, null, null);
if(albumCursor != null && albumCursor.moveToFirst()) {
int albumName = albumCursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM);
int albumArtist = albumCursor.getColumnIndex(MediaStore.Audio.Albums.ARTIST);
int albumID = albumCursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ID);
do {
String thisAlbumName = albumCursor.getString(albumName);
String thisAlbumArtist = albumCursor.getString(albumArtist);
String thisAlbumID = albumCursor.getString(albumID);//this line is giving me an error
temp.add(new Album(thisAlbumName, thisAlbumArtist, thisAlbumID));
}while(albumCursor.moveToNext());
}
return temp;
}
I too faced this issue. You can easily solve this issue by changing
int albumID = albumCursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ID);
TO
int albumID = albumCursor.getColumnIndex(MediaStore.Audio.Albums._ID);
That is you need to use _ID whenever you want AlbumID of the Album. I have no idea why MediaStore developers used _ID instead of ALBUM_ID, etc stuff.
I'm developing app to provide and set pictures for contacts from social networks, and I'm already figure out how to set picture to contact, but when I do this programmatically picture is stored in lower quality.
For example here is 2 examples, first picture is set with my app, second with native android contacts app (source picture is exactly the same for both cases):
First example
With my app:
With native app:
Second example
With my app:
With native app:
You can see pixilization on the hands and other parts.
The code I've used to set contact picture:
public static boolean setContactPhoto(long contactId, byte[] photo) {
ContentResolver cr = context.getContentResolver();
ContentValues values = new ContentValues();
long photoId = -1;
long rawContactId = -1;
Cursor rawContactsCursor = cr.query(
ContactsContract.RawContacts.CONTENT_URI,
new String[]{ContactsContract.RawContacts._ID},
String.format("%s=%d", ContactsContract.RawContacts.CONTACT_ID, contactId),
null,
null
);
while (rawContactsCursor.moveToNext()) {
rawContactId = rawContactsCursor.getLong(rawContactsCursor.getColumnIndex(ContactsContract.RawContacts._ID));
String where = String.format(
"%s=%d AND %s=='%s'",
ContactsContract.Data.RAW_CONTACT_ID,
rawContactId,
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE
);
Cursor dataCursor = cr.query(
ContactsContract.Data.CONTENT_URI,
new String[]{ContactsContract.Data._ID},
where,
null,
null
);
if (dataCursor.moveToFirst()) {
photoId = dataCursor.getLong(dataCursor.getColumnIndex(ContactsContract.Data._ID));
dataCursor.close();
break;
}
dataCursor.close();
}
rawContactsCursor.close();
if (rawContactId < 0) return false;
values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
values.put(ContactsContract.Data.IS_SUPER_PRIMARY, 1);
values.put(ContactsContract.CommonDataKinds.Photo.PHOTO, photo);
values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
if (photoId < 0) {
return cr.insert(ContactsContract.Data.CONTENT_URI, values) != null;
} else {
return cr.update(ContactsContract.Data.CONTENT_URI, values, String.format("%s=%d", ContactsContract.Data._ID, photoId), null) == 1;
}
}
My phone has 540x960 resolution, so source pictures are 960x960, this is strange, but if I try to set 540x960 picture it is cropped from all sides and scaled up, so pixilization is bigger.
So, how to avoid such pixilization setting up contact picture programmatically?
As described in the documentation, the .photo field hold the "Thumbnail photo of the raw contact."
I think you have to set the PHOTO_FILE_ID used to "accessing full-size photos by photo file ID".
See the related ContactsContract.DisplayPhoto
How can I get an exact count of MMS inbox and Sent box in ANdroid?
Uri mmsInboxUri = Uri.parse("content://mms/inbox");
Cursor mmsInboxCursor = getContentResolver().query(mmsInboxUri ,new String[] {"_id","ct_t"}, null, null, null);
int count = mmsInboxCursor.getCount();
if (mmsInboxCursor != null)
{
if (mmsInboxCursor.moveToFirst())
{
coloumns = mmsInboxCursor.getColumnNames();
//String subject = mmsInboxCursor.getString(mmsInboxCursor.getColumnIndex("sub"));
String string = mmsInboxCursor.getString(1);
if ("application/vnd.wap.multipart.related".equals(string))