I'm trying to change the album art of an audio file using jaudiotagger but it is not working. There are a lot of question regarding this topic but none of them fulfilled my requirements. Other fields such as artist,audio name, album etc are updated successfully but the album artwork remains the same.
The code I'm using to change the artwork:
public void changeAlbumArt(Uri data) {
Artwork cover = null;
try {
AudioFile f = null;
try {
f = AudioFileIO.read(new File(storageUtil.loadAudio().get(storageUtil.loadAudioIndex()).getData()));
} catch (CannotReadException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (TagException e) {
e.printStackTrace();
} catch (ReadOnlyFileException e) {
e.printStackTrace();
} catch (InvalidAudioFrameException e) {
e.printStackTrace();
}
Tag tag = null;
if (f != null) {
tag = f.getTag();
}
try {
if(tag!=null) {
cover = ArtworkFactory.createArtworkFromFile(new File(data.getPath()));
tag.deleteArtworkField();
tag.setField(cover);
}
} catch (FieldDataInvalidException e) {
e.printStackTrace();
}
try {
if (f != null) {
f.commit();
}
} catch (CannotWriteException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
File file = new File(storageUtil.loadAudio().get(storageUtil.loadAudioIndex()).getData());
Uri uri = Uri.fromFile(file);
Intent scanFileIntent = new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri);
sendBroadcast(scanFileIntent);
}
I had this working at one point but have reverted back to jid3 as jaudiotagger required some io files which were not available in android.
In addition, for android Gingerbread+ to write to external storage requires the SAF framework. I have managed to modify the jid3 library so it works but only for mp3.
for mp3 files:
public String setMp3AlbumArt(File SourceFile, byte[] bytes)
throws Exception {
String error = null;
try {
MP3File musicFile = (MP3File) AudioFileIO.read(SourceFile);
AbstractID3v2Tag tag = musicFile.getID3v2Tag();
if (tag != null) {
Artwork artwork = null;
try {
artwork = ArtworkFactory.createArtworkFromFile(SourceFile);
} catch (IOException e) {
e.printStackTrace();
error = e.getMessage();
}
if (artwork != null) {
artwork.setBinaryData(bytes);
tag.deleteArtworkField();
tag.setField(artwork);
musicFile.setTag(tag);
musicFile.commit();
} else {
artwork.setBinaryData(bytes);
tag.addField(artwork);
tag.setField(artwork);
musicFile.setTag(tag);
musicFile.commit();
}
}
} catch (CannotReadException | IOException | TagException
| ReadOnlyFileException | InvalidAudioFrameException e) {
error = e.getMessage();
}
return error;
}
Related
I have a binary file in root of apk and I can check it by:
AssetManager assets = Context().getResources().getAssets();
assets.list("/")
It exists in returning list. But how can I open it?
Because when I try to open the file by using this code:
assets.open("/test.bin")
I faced by FileNotFoundException.
finally, I soloved it by using getClass().getResourceAsStream() instead of assetManager:
public byte[] loadBinAsset(String name) {
AssetManager assets = context.getResources().getAssets();
InputStream stream = null;
try {
try {
stream = assets.open(name);
} catch (IOException e) {
stream = context.getClass().getResourceAsStream("/" + name);
}
return readFully(stream);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (stream != null) {
stream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
here is string to setDataSource
/data/data/com.player/app_player/file.mp3
getting E/Exception: setDataSource failed.
here is the code:
mediaPlayer.setDataSource(context, Uri.parse("/data/data/com.player/app_player/file.mp3"));
I stored that file using this code
getContext().getDir("player", Context.MODE_PRIVATE)
which is same as /data/data/com.player/app_player
using content://data/data/com.player/app_player/file.mp3 did not work.
This is how I solve it.
String musicUrl = "";
if (songSavedInDB()) {
musicUrl = "here is any file path(Internal or external)"
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(musicUrl);
mPlayer.setDataSource(fileInputStream.getFD());
fileInputStream.close();
mPlayer.prepare();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
musicUrl = "here is url";
try {
mPlayer.setDataSource(getContext(), Uri.parse(musicUrl));
mPlayer.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
}
So I am trying to edit the tags of music files in my app. This is the code
TagOptionSingleton.getInstance().setAndroid(true);
AudioFile f = AudioFileIO.read(sel);
Tag tag = f.getTag();
tag.setField(FieldKey.TITLE, "ABC");
f.commit();
I have tried AudioFileIO.write(f) instead of f.commit() but the changes that I make are not reflected in the files. They still have the same old data.
I have tried a number of versions of JAudioTagger but no luck. Can someone please help me. Thanks in advance !!
I know it is a bit late but the following works for me (I use the same framework for the other tags
public String getmp3TrackTitleTag(File SourceFile)
throws Exception {
String TrackTitle = null;
try {
MP3File musicFile = (MP3File) AudioFileIO.read(SourceFile);
if (musicFile != null && musicFile.hasID3v2Tag()) {
ID3v24Tag id3v24 = (ID3v24Tag) musicFile.getID3v2TagAsv24();
TrackTitle = id3v24.getFirst(ID3v24Frames.FRAME_ID_TITLE);
}
} catch (CannotReadException | IOException | TagException
| ReadOnlyFileException | InvalidAudioFrameException e5) {
throw e5;
}
return TrackTitle;
}
and to set the title
public String setmp3TrackTitleTag(File SourceFile, String strTrackTitle)
throws Exception {
String error = null;
AbstractID3v2Tag v2tag = null;
try {
MP3File musicFile = (MP3File) AudioFileIO.read(SourceFile);
if (musicFile != null && musicFile.hasID3v2Tag()) {
v2tag = musicFile.getID3v2Tag();
try {
v2tag.setField(FieldKey.TITLE, strTrackTitle);
musicFile.setTag(v2tag);
musicFile.commit();
} catch (KeyNotFoundException e) {
e.printStackTrace();
error = e.getMessage();
} catch (FieldDataInvalidException e) {
e.printStackTrace();
error = e.getMessage();
} catch (CannotWriteException e) {
e.printStackTrace();
error = e.getMessage();
}
}
} catch (CannotReadException | IOException | TagException
| ReadOnlyFileException | InvalidAudioFrameException e5) {
throw e5;
}
return error;
}
I have written a Web View app, which logs you into 12 different sites (sign in) which works pretty fine. However, i am trying to figure out a way to backup my web view's data (so that all the login credentials are saved) to SD card. the only way i have found is to copy the root/data/data/com.example/your app folder.
How do i copy this folder somewhere to my SD card using root command on the click of a button?
this is how i access and delete the data folder
private void clear() {
String cmd = "pm clear com.wagtailapp";
ProcessBuilder pb = new ProcessBuilder().redirectErrorStream(true)
.command("su");
Process p = null;
try {
p = pb.start();
} catch (IOException e) {
e.printStackTrace();
}
StreamReader stdoutReader = new StreamReader(p.getInputStream(),
CHARSET_NAME);
stdoutReader.start();
out = p.getOutputStream();
try {
out.write((cmd + "\n").getBytes(CHARSET_NAME));
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
out.write(("exit" + "\n").getBytes(CHARSET_NAME));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
p.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
String result = stdoutReader.getResult();
}
}
streamreader.java
class StreamReader extends Thread {
private InputStream is;
private StringBuffer mBuffer;
private String mCharset;
private CountDownLatch mCountDownLatch;
StreamReader(InputStream is, String charset) {
this.is = is;
mCharset = charset;
mBuffer = new StringBuffer("");
mCountDownLatch = new CountDownLatch(1);
}
String getResult() {
try {
mCountDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
return mBuffer.toString();
}
#Override
public void run() {
InputStreamReader isr = null;
try {
isr = new InputStreamReader(is, mCharset);
int c = -1;
while ((c = isr.read()) != -1) {
mBuffer.append((char) c);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (isr != null)
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
mCountDownLatch.countDown();
}
}
}
I want to list all the files that my google accouunts have with my app, i tried the follow piece of code which is actually from the website, but , there's error in the line " FileList fileList = request.execute(); ", it says the method execute() is undefined for the type Drive.Files.List ,i don't know how to fix it.
private void getDriveContents()
{
Thread t = new Thread(new Runnable()
{
#Override
public void run()
{
com.google.api.services.drive.Drive.Files f1 = mService.files();
Files.List request = null;
do
{
try
{
request = service.files().list().setQ("trashed=false");
FileList fileList = request.execute();
mResultList.addAll(fileList.getItems());
request.setPageToken(fileList.getNextPageToken());
} catch (UserRecoverableAuthIOException e) {
startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
} catch (IOException e) {
e.printStackTrace();
if (request != null)
{
request.setPageToken(null);
}
}
} while (request.getPageToken() !=null && request.getPageToken().length() > 0);
populateListView();
}
});
t.start();
}
I believe your 'request' is of incorrect type. it is supposed to be FileList, not File.List
I wasn't arguing about what you've seen. I was only trying to help since I have similar code running and tested. Here's a snippet from my "class ListDownFromGOODrive extends AsyncTask..."
import com.google.api.services.drive.model.FileList;
...
.... a lot of code here
...
GoogleAccountCredential _crd;
Drive _svc;
FileList _gooLst;
Intent _it;
String _rqst;
...
... a lot of code here
...
#Override protected Integer doInBackground(Void... nothing) {
try {
if (_crd == null)
_crd = GoogleAccountCredential.usingOAuth2(_ctx,Arrays.asList(DriveScopes.DRIVE_FILE));
if (_svc == null)
_svc = new Drive.Builder
(AndroidHttp.newCompatibleTransport(), new GsonFactory(), _crd).build();
if (_crd.getSelectedAccountName() == null) {
_it = _crd.newChooseAccountIntent();
return REQ_EMAIL;
}
_gooLst = _svc.files().list().setMaxResults(MAX_DOWN).setQ(_rqst)
.setFields("items(id,title,description,downloadUrl,thumbnailLink)").execute();
}
catch (UserRecoverableAuthIOException e){ //Log.d("atn", "LD URAIO Except->REQ_AUTH");
try { _it = e.getIntent(); } catch (Exception e1) {return ERROR;}
return REQ_AUTH;
}
catch (IOException e) { return ERROR; }
catch (Exception e) { return ERROR; }
return GOOD;
}
it returns GOOD, ERROR, REQ_AUTH(orization), or REQ_EMAIL(account)