Last active
August 29, 2015 14:24
-
-
Save itzikBraun/081a749a8736d0b4d3d4 to your computer and use it in GitHub Desktop.
RequestHandlers for Picasso. Blog post about RequestHandlers: http://lucasr.org/2014/09/23/new-features-in-picasso/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.media.MediaMetadataRetriever; | |
import com.squareup.picasso.Picasso; | |
import com.squareup.picasso.Request; | |
import com.squareup.picasso.RequestHandler; | |
import java.io.ByteArrayInputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
/** | |
* Created by braunster on 13.07.15. | |
*/ | |
public class ArtworkRequestHandler extends RequestHandler { | |
@Override | |
public boolean canHandleRequest(Request data) { | |
return data.uri != null && (data.uri.getScheme() == null || data.uri.getScheme().equals("file")) && (data.uri.getPath().contains(".mp3")); | |
} | |
@Override | |
public Result load(Request request, int networkPolicy) throws IOException { | |
MediaMetadataRetriever myRetriever = new MediaMetadataRetriever(); | |
myRetriever.setDataSource(request.uri.getPath()); // the URI of audio file | |
byte[] artwork = myRetriever.getEmbeddedPicture(); | |
if (artwork == null || artwork.length < 0) { | |
throw new IOException("cant load artwork"); | |
} | |
InputStream is = new ByteArrayInputStream(artwork); | |
return new Result(is, Picasso.LoadedFrom.DISK); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Picasso getInstance(Context context){ | |
return new Picasso.Builder(context) | |
.indicatorsEnabled(true) | |
.addRequestHandler(new ArtworkRequestHandler()) | |
.addRequestHandler(new VideoRequestHandler()) | |
.build(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.media.ThumbnailUtils; | |
import android.provider.MediaStore; | |
import com.squareup.picasso.Picasso; | |
import com.squareup.picasso.Request; | |
import com.squareup.picasso.RequestHandler; | |
import java.io.IOException; | |
/** | |
* Created by braunster on 14.07.15. | |
*/ | |
public class VideoRequestHandler extends RequestHandler { | |
@Override | |
public boolean canHandleRequest(Request data) { | |
return data.uri != null && (data.uri.getScheme() == null || data.uri.getScheme().equals("file")) && (data.uri.getPath().contains(".mp4")); | |
} | |
@Override | |
public Result load(Request request, int networkPolicy) throws IOException { | |
return new Result(ThumbnailUtils.createVideoThumbnail(request.uri.getPath(), MediaStore.Images.Thumbnails.MINI_KIND), Picasso.LoadedFrom.DISK); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment