/** ~~~~~ * Created with the AppyBuilder Code Editor. https://editor.appybuilder.com/ * **** NOTE: DO NOT use a package name. * **** The package name will be created for you automatically. * **** Adding a package name will cause a compile error */ import android.content.Context; import android.util.Log; import com.google.appinventor.components.annotations.*; import com.google.appinventor.components.runtime.*; import com.google.appinventor.components.common.ComponentCategory; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.media.MediaMetadataRetriever; import com.google.appinventor.components.common.PropertyTypeConstants; import com.google.appinventor.components.runtime.util.MediaUtil; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; @DesignerComponent(version = 2, description = "obtention des tags d'un mp3" + " par jm latour ", category = ComponentCategory.EXTENSION, nonVisible = true, iconName = "https://www.dropbox.com/s/7jim1f21jjxtv4v/logo%20Part-cours.png?dl=0") @SimpleObject(external = true) public class Mp3tags extends AndroidNonvisibleComponent { private ComponentContainer container; public static String fichier=""; /** * @param container container, component will be placed in */ public Mp3tags(ComponentContainer container) { super(container.$form()); this.container = container; } MediaMetadataRetriever metaRetriever; @SimpleFunction(description = "return file's artist") public String Artist(String song){ song=defineDir(song); File file=new File(song); metaRetriever = new MediaMetadataRetriever(); if (file.exists()) { metaRetriever.setDataSource(song); try { song=metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST); if (song == ""|| song == null) { song = ""; } } catch (Exception e) { song=" Exception"; } }else { song="file not found"; } return song; } @SimpleFunction(description = "return file's album") public String Album(String song){ song=defineDir(song); File file=new File(song); metaRetriever = new MediaMetadataRetriever(); if (file.exists()) { metaRetriever.setDataSource(song); try { song=metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM); if (song == ""|| song == null) { song = ""; } } catch (Exception e) { song=" Exception"; } }else { song="file not found"; } return song; } @SimpleFunction(description = "return file's title") public String Title(String song){ song=defineDir(song); File file=new File(song); metaRetriever = new MediaMetadataRetriever(); if (file.exists()) { metaRetriever.setDataSource(song); try { song=metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE); if (song == ""|| song == null) { song = ""; } } catch (Exception e) { song=" Exception"; } }else { song="file not found"; } return song; } @SimpleFunction(description = "return file's genre") public String Genre(String song){ song=defineDir(song); File file=new File(song); metaRetriever = new MediaMetadataRetriever(); if (file.exists()) { metaRetriever.setDataSource(song); try { song=metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE); if (song == ""|| song == null) { song = ""; } } catch (Exception e) { song=" Exception"; } }else { song="file not found"; } return song; } @SimpleFunction(description = "return file's path") public String Path(String song){ String path=defineDir(song); //path=form.getExternalFilesDir("Mp3Tags").getAbsolutePath()+"/tmp.png"; return path; } String defineDir(String path){ if(path.startsWith("//")){ return "/storage/emulated/0/AppInventor/assets/"+path.substring(2,path.length()); }else{ if(path.startsWith("/")){ return "/mnt/sdcard"+path; }else { if (path.startsWith("file:///") ) { return path.substring(7,path.length()); } else return path; } } } @SimpleFunction(description = "return file's album art") public String Album_Art(String song){ song=defineDir (song); File file=new File(song); byte[] art = new byte[0]; Bitmap songImage=null; metaRetriever = new MediaMetadataRetriever(); if (file.exists()) { metaRetriever.setDataSource(song); try {song="try"; art = metaRetriever.getEmbeddedPicture(); songImage = BitmapFactory .decodeByteArray(art, 0, art.length); } catch (Exception e) {song="exception"; } }else { //song="file not found : "+ file.toString(); } String filename =form.getExternalFilesDir("Mp3Tags").getAbsolutePath()+"/tmp.png"; String reponse=saveBitmap(songImage,filename); return reponse; } private String saveBitmap(Bitmap bitmap, String path) { File file = null; String reponse="vide"; if (bitmap != null) { file = new File(path); try { FileOutputStream outputStream = null; try { outputStream = new FileOutputStream(path); //here is set your file path where you want to save or also here you can set file object directly bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream); // bitmap is your Bitmap instance, if you want to compress it you can compress reduce percentage // PNG is a lossless format, the compression factor (100) is ignored reponse=file.toString(); } catch (Exception e) { e.printStackTrace(); reponse="erreur"; } finally { try { if (outputStream != null) { outputStream.close(); } } catch (IOException e) { //e.printStackTrace(); } } } catch (Exception e) { e.printStackTrace(); reponse="erreur 2"; } }else{reponse="null";} return reponse; } }