Adding mp3 to ContentResolver

I know that after downloading mp3 from your application you need to add it to ContentResolver in order to see it on the music player. I am doing this with the following code:

  private void addFileToContentProvider() { ContentValues values = new ContentValues(7); values.put(Media.DISPLAY_NAME, "display_name"); values.put(Media.ARTIST, "artist"); values.put(Media.ALBUM, "album"); values.put(Media.TITLE, "Title"); values.put(Media.MIME_TYPE, "audio/mp3"); values.put(Media.IS_MUSIC, true); values.put(Media.DATA, pathToFile); context.getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values); } 

My problem is that I am ready to avoid setting DISPLAY_NAME , ARTIST , ALBUM , TITLE manually.

Is there any way to tell Android to do this from a file? I already used only values.put(Media.DATA, pathToFile); but did not add it to the player.

Is there a way to force a stream that scans sd for music?

+3
source share
1 answer

First try adding this:

 values.put(MediaStore.Audio.Media.IS_MUSIC, true); 

Then try the following:

 sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename))); 

If this does not work, try the following:

 public MediaScannerConnection mScanner = null; public void reScan(String filename){ final String name = filename; mScanner = new MediaScannerConnection(THE_NAME_OF_YOUR_ACTIVITY.this, new MediaScannerConnection.MediaScannerConnectionClient() { public void onMediaScannerConnected() { mScanner.scanFile(name, null); } public void onScanCompleted(String path, Uri uri) { if (path.equals(name)) { mScanner.disconnect(); } } }); mScanner.connect(); } 

After adding mp3, just call this method, which takes the file name as input.

And don't forget to replace THE_NAME_OF_YOUR_ACTIVITY with the name of your activity.

+4
source

Source: https://habr.com/ru/post/891407/


All Articles