Skip to content Skip to sidebar Skip to footer

How To Set Our Image As Artwork In A Mp3 Album In Android

I am creating an application in which we want to insert an artwork for which .mp3 file which have no any artwork. as well as i also want to replace old artwork with new image taken

Solution 1:

Hope this help you-

ContentResolverres= context.getContentResolver();
    Uriuri= ContentUris.withAppendedId(sArtworkUri, album_id);
    if (uri != null) {
        InputStreamin=null;
        try {
            in = res.openInputStream(uri);
            return BitmapFactory.decodeStream(in, null, sBitmapOptions);
        } catch (FileNotFoundException ex) {
            // The album art thumbnail does not actually exist. Maybe the user deleted it, or// maybe it never existed to begin with.Bitmapbm= getArtworkFromFile(context, null, album_id);
            if (bm != null) {
                // Put the newly found artwork in the database.// Note that this shouldn't be done for the "unknown" album,// but if this method is called correctly, that won't happen.// first write it somewhereStringfile= Environment.getExternalStorageDirectory()
                    + "/albumthumbs/" + String.valueOf(System.currentTimeMillis());
                if (ensureFileExists(file)) {
                    try {
                        OutputStreamoutstream=newFileOutputStream(file);
                        if (bm.getConfig() == null) {
                            bm = bm.copy(Bitmap.Config.RGB_565, false);
                            if (bm == null) {
                                return getDefaultArtwork(context);
                            }
                        }
                        booleansuccess= bm.compress(Bitmap.CompressFormat.JPEG, 75, outstream);
                        outstream.close();
                        if (success) {
                            ContentValuesvalues=newContentValues();
                            values.put("album_id", album_id);
                            values.put("_data", file);
                            Urinewuri= res.insert(sArtworkUri, values);
                            if (newuri == null) {
                                // Failed to insert in to the database. The most likely// cause of this is that the item already existed in the// database, and the most likely cause of that is that// the album was scanned before, but the user deleted the// album art from the sd card.// We can ignore that case here, since the media provider// will regenerate the album art for those entries when// it detects this.
                                success = false;
                            }
                        }
                        if (!success) {
                            Filef=newFile(file);
                            f.delete();
                        }
                    } catch (FileNotFoundException e) {
                        Log.e(TAG, "error creating file", e);
                    } catch (IOException e) {
                        Log.e(TAG, "error creating file", e);
                    }
                }
            } else {
                bm = getDefaultArtwork(context);
            }
            return bm;
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
            }
        }
    }

Ans Taken from - How can I update the album art path using contentResolver?

Post a Comment for "How To Set Our Image As Artwork In A Mp3 Album In Android"