I'm recording video with my app and on stop the video is added to Android MediaStore in order to appear in the video gallery.
I need to get the thumbnail URI as soon the video is added to the gallery.
How I can do this ?
Below, the code I'm using to add the video to gallery:
private void addVideoGallery() {
long dateTaken=System.currentTimeMillis();
MediaPlayer mp = MediaPlayer.create(this, Uri.parse(outputfilename));
int duration = mp.getDuration();
mp.release();
ContentValues values = new ContentValues();
values.put(MediaStore.Video.Media.DATA, outputfilename);
values.put(MediaStore.Video.Media.DATE_TAKEN, dateTaken);
values.put(MediaStore.Video.Media.DURATION, duration);
ContentResolver resolver = MainActivity.this.getContentResolver();
resolver.insert(Video.Media.EXTERNAL_CONTENT_URI, values);
}
Related
I'm trying to set the android default ringtone or notification tone via content provider from my assets folder.
Surprisingly, it works like this, but is it a legitimate way?
Uri audiouri = Uri.parse("content://"+BuildConfig.APPLICATION_ID+"/"+soundname+".mp3");
RingtoneManager.setActualDefaultRingtoneUri(a, TYPE_NOTIFICATION, audiouri );
Unfortunately, the sound name isn't shown in Android settings.
Strangely the sound name is actually shown when I go to 'Other sounds'
I also tried this:
Uri audiouri = Uri.parse("content://"+BuildConfig.APPLICATION_ID+"/"+soundname+".mp3");
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.MediaColumns.TITLE, soundname);
Uri ringtoneuri = a.getContentResolver().insert(audiouri, contentValues);
RingtoneManager.setActualDefaultRingtoneUri(a, TYPE_NOTIFICATION, ringtoneuri);
resulting in a null sound (no sound is set)
third option I tried is:
Uri audiouri = MediaStore.Audio.Media.getContentUriForPath("content://"+BuildConfig.APPLICATION_ID+"/"+soundname+".mp3");
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.MediaColumns.DATA, "content://"+BuildConfig.APPLICATION_ID+"/"+soundname+".mp3");
contentValues.put(MediaStore.MediaColumns.TITLE, soundname);
Uri ringtoneuri = a.getContentResolver().insert(audiouri, contentValues);
RingtoneManager.setActualDefaultRingtoneUri(a, TYPE_NOTIFICATION, ringtoneuri);
Now the sound name is shown correctly, but no sound is actually played.
I get error on logcat:
java.io.FileNotFoundException: Can't access /content:/com.mydomain.myapp/test.mp3
So it seems it's taking the value from MediaColumns.DATA which does not support Content provider paths but only real paths. Right?
Final question: How to set tone AND name in android settings? Preferably without copying the file to external storage.
So, unfortunately I did not find out how to set asset as ringtone directly,
but this is a nice workaround:
When copying asset to internal app storage or cache dir (no permissions needed for that!) I was able to set the ringtone without WRITE_EXTERNAL_STORAGE permisson.
static void settone(int type, Sound sound, Activity a)
{
lastsound = sound; //global remember sound and type (alarm/ringtone/notification)
lasttype = type; // if we have to get permissions first, then call this from onActivityResult
if (canwritesystem(a))
{
RingtoneManager.setActualDefaultRingtoneUri(a, type, getringtoneuri(sound, a));
Toast.makeText(a, a.getString(R.string.settonesuccess), Toast.LENGTH_LONG).show();
}
else a.startActivityForResult(new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS).setData(Uri.parse("package:" + a.getPackageName())),CONTEXT_SET_TONE);
}
static Uri getringtoneuri(Sound sound, Activity a)
{
File tonefile = new File(sound.getpath); // path could be like: /Android/data/com.company.yourapp
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.MediaColumns.DATA, tonefile.getAbsolutePath());
contentValues.put(MediaStore.MediaColumns.TITLE, sound.getDisplayName());
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
contentValues.put(MediaStore.MediaColumns.SIZE, tonefile.length());
contentValues.put(MediaStore.Audio.Media.IS_RINGTONE, true);
contentValues.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
contentValues.put(MediaStore.Audio.Media.IS_ALARM, true);
contentValues.put(MediaStore.Audio.Media.IS_MUSIC, false);
Uri generalaudiouri = MediaStore.Audio.Media.INTERNAL_CONTENT_URI;
a.getContentResolver().delete(generalaudiouri, MediaStore.MediaColumns.DATA + "='" + tonefile.getAbsolutePath() + "'", null);
return a.getContentResolver().insert(generalaudiouri, contentValues);
}
I am using Jaudiotagger 2.2.5 for an android music tagging app. I am able to change metadata like album name, artist name, genre etc. But no matter what I try I can't get the album art part working. I have exhausted all suggestions that I could find online, but nothing seems to work. Jaudiotagger itself lacks documentation nor is the developer very helpful answering such issues.
for(Song s : songlist){ //for each song in the album
File file = new File(artUri);
if(file.exists()) {
Artwork cover = ArtworkFactory.createArtworkFromFile(file);
tag.deleteArtworkField();
tag.createField(cover);
tag.setField(cover);
af.commit();
}
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(f));
sendBroadcast(intent);
}
Additionally I am using another method to update the mediastore:
public void updateAlbumArtMediaStore(Context context, final long id, String art){
Uri uri = ContentUris.withAppendedId(Uri.parse("content://media/external/audio/albumart"), id);
context.getContentResolver().delete(uri,null, null);
ContentValues values = new ContentValues();
values.put("album_id", id);
values.put("_data", art);
Uri newuri = context.getContentResolver()
.insert(Uri.parse("content://media/external/audio/albumart"),
values);
if(newuri!=null){
Toast.makeText(AlbumTagEditorActivity.this, "UPDATED", Toast.LENGTH_LONG).show();
context.getContentResolver().notifyChange(uri, null);
}else{
Toast.makeText(AlbumTagEditorActivity.this, "FAILED", Toast.LENGTH_LONG).show();
}
}
But when I do this it only deletes the cover art.
Its been months since I am trying to get this right. Nothing I have tried ever worked.
Here is my code. First I recorded an audio file and started playing it. For playing the audio file
/**
*
* play the recorded audio
*
*/
public void playAudio() {
try {
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
Uri data = Uri.parse(path);
intent.setDataAndType(data, "audio/mp3");
startActivity(intent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
// TODO: handle exception
}
}
and the output is calling the default media player of device. Here I need to show an image in that media player in the place of album image.
Here is an image of what I need:
The default media player is using the following code to retrieve the album art of a song.
Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
ContentResolver res = context.getContentResolver();
InputStream in = res.openInputStream(uri);
Bitmap artwork = BitmapFactory.decodeStream(in);
where album_id is the albumd id of the song. So in order to show an album art the default player should know the album id and check if there is album art associated with this id. So if you want to show artwork you have to insert your file in android's database and then play it from there.
Using below code i captured Video,but how to get duration of captured video in android ?
and also when sometimes user discard video and record new video then get duration of new video .
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(cameraIntent, TAKE_VIDEO);
I think the easiest way is:
MediaPlayer mp = MediaPlayer.create(this, Uri.parse(uriOfFile);
int duration = mp.getDuration();
mp.release();
/*convert millis to appropriate time*/
return String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes(duration),
TimeUnit.MILLISECONDS.toSeconds(duration) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(duration))
);
Using cursor you can get duration and there are duration Column in Cursor and you can get as a string.
Cursor cursor = MediaStore.Video.query(getContentResolver(),data.getData(),
new String[] { MediaStore.Video.VideoColumns.DURATION });
System.out.println(">>>>>>>>>>"+cursor.getCount());
cursor.moveToFirst();
String duration = cursor.getString(cursor.getColumnIndex("duration"));
MediaPlayer is a heavy object. Use MediaMetadataRetriever instead
fun getMediaDurationMs(context: Context, fileUri: Uri): Int? {
val mmr = MediaMetadataRetriever()
mmr.setDataSource(context, fileUri)
return mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)?.toInt()
}
Here is my code. First I recorded an audio file and started playing it. For playing the audio file
/**
*
* play the recorded audio
*
*/
public void playAudio() {
try {
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
Uri data = Uri.parse(path);
intent.setDataAndType(data, "audio/mp3");
startActivity(intent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
// TODO: handle exception
}
}
and the output is calling the default media player of device. Here I need to show an image in that media player in the place of album image.
Here is an image of what I need:
The default media player is using the following code to retrieve the album art of a song.
Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
ContentResolver res = context.getContentResolver();
InputStream in = res.openInputStream(uri);
Bitmap artwork = BitmapFactory.decodeStream(in);
where album_id is the albumd id of the song. So in order to show an album art the default player should know the album id and check if there is album art associated with this id. So if you want to show artwork you have to insert your file in android's database and then play it from there.