/** * Start an image request using the specified image file. This is a convenience method for * calling {@link #load(Uri)}. * <p> * Passing {@code null} as a {@code file} will not trigger any request but will set a * placeholder, if one is specified. * <p> * Equivalent to calling {@link #load(Uri) load(Uri.fromFile(file))}. * * @see #load(Uri) * @see #load(String) * @see #load(int) */ @NonNull public RequestCreator load(@Nullable File file) { if (file == null) { return new RequestCreator(this, null, 0); } return load(Uri.fromFile(file)); }
/** * Start an image request using the specified path. This is a convenience method for calling * {@link #load(Uri)}. * <p> * This path may be a remote URL, file resource (prefixed with {@code file:}), content resource * (prefixed with {@code content:}), or android resource (prefixed with {@code android.resource:}. * <p> * Passing {@code null} as a {@code path} will not trigger any request but will set a * placeholder, if one is specified. * * @throws IllegalArgumentException if {@code path} is empty or blank string. * @see #load(Uri) * @see #load(File) * @see #load(int) */ @NonNull public RequestCreator load(@Nullable String path) { if (path == null) { return new RequestCreator(this, null, 0); } if (path.trim().length() == 0) { throw new IllegalArgumentException("Path must not be empty."); } return load(Uri.parse(path)); }
@Override public void bindView(View view, Context context, Cursor cursor) { Uri contactUri = Contacts.getLookupUri(cursor.getLong(ContactsQuery.ID), cursor.getString(ContactsQuery.LOOKUP_KEY)); ViewHolder holder = (ViewHolder) view.getTag(); holder.text1.setText(cursor.getString(ContactsQuery.DISPLAY_NAME)); holder.icon.assignContactUri(contactUri); PicassoProvider.get() .load(contactUri) .placeholder(R.drawable.contact_picture_placeholder) .tag(context) .into(holder.icon); }
@Override public RequestHandler.Result transform(RequestHandler.Result source) { Bitmap bitmap = source.getBitmap(); if (bitmap == null) { return source; } Bitmap result = createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig()); Bitmap noise; try { noise = picasso.load(R.drawable.noise).get(); } catch (IOException e) { throw new RuntimeException("Failed to apply transformation! Missing resource."); } BitmapShader shader = new BitmapShader(noise, REPEAT, REPEAT); ColorMatrix colorMatrix = new ColorMatrix(); colorMatrix.setSaturation(0); ColorMatrixColorFilter filter = new ColorMatrixColorFilter(colorMatrix); Paint paint = new Paint(ANTI_ALIAS_FLAG); paint.setColorFilter(filter); Canvas canvas = new Canvas(result); canvas.drawBitmap(bitmap, 0, 0, paint); paint.setColorFilter(null); paint.setShader(shader); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY)); canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), paint); bitmap.recycle(); noise.recycle(); return new RequestHandler.Result(result, source.getLoadedFrom(), source.getExifRotation()); }
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { SampleListDetailActivity activity = (SampleListDetailActivity) getActivity(); View view = LayoutInflater.from(activity) .inflate(R.layout.sample_list_detail_detail, container, false); TextView urlView = view.findViewById(R.id.url); ImageView imageView = view.findViewById(R.id.photo); Bundle arguments = getArguments(); String url = arguments.getString(KEY_URL); urlView.setText(url); PicassoProvider.get() .load(url) .fit() .tag(activity) .into(imageView); return view; } }
@Override public void onUpdate(final Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.sample_widget); // Load image for all appWidgetIds. Picasso picasso = PicassoProvider.get(); picasso.load(Data.URLS[new Random().nextInt(Data.URLS.length)]) // .placeholder(R.drawable.placeholder) // .error(R.drawable.error) // .transform(new GrayscaleTransformation(picasso)) // .into(updateViews, R.id.image, appWidgetIds); } }
@Override public View getView(int position, View view, ViewGroup parent) { ViewHolder holder; if (view == null) { view = LayoutInflater.from(context).inflate(R.layout.sample_list_detail_item, parent, false); holder = new ViewHolder(); holder.image = view.findViewById(R.id.photo); holder.text = view.findViewById(R.id.url); view.setTag(holder); } else { holder = (ViewHolder) view.getTag(); } // Get the image URL for the current position. String url = getItem(position); holder.text.setText(url); // Trigger the download of the URL asynchronously into the image view. PicassoProvider.get() .load(url) .placeholder(R.drawable.placeholder) .error(R.drawable.error) .resizeDimen(R.dimen.list_detail_image_size, R.dimen.list_detail_image_size) .centerInside() .tag(context) .into(holder.image); return view; }
@Test public void loadThrowsWithInvalidInput() { try { picasso.load(""); fail("Empty URL should throw exception."); } catch (IllegalArgumentException expected) { } try { picasso.load(" "); fail("Empty URL should throw exception."); } catch (IllegalArgumentException expected) { } try { picasso.load(0); fail("Zero resourceId should throw exception."); } catch (IllegalArgumentException expected) { } }
@Override public void launch(Activity activity) { RemoteViews remoteViews = new RemoteViews(activity.getPackageName(), R.layout.notification_view); Intent intent = new Intent(activity, SampleGridViewActivity.class); Notification notification = new NotificationCompat.Builder(activity, CHANNEL_ID) .setSmallIcon(R.drawable.icon) .setContentIntent(PendingIntent.getActivity(activity, -1, intent, 0)) .setContent(remoteViews) .setAutoCancel(true) .setChannelId(CHANNEL_ID) .build(); NotificationManager notificationManager = (NotificationManager) activity.getSystemService(Context.NOTIFICATION_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "Picasso Notification Channel", NotificationManager.IMPORTANCE_DEFAULT); notificationManager.createNotificationChannel(channel); } notificationManager.notify(NOTIFICATION_ID, notification); // Now load an image for this notification. PicassoProvider.get() // .load(Data.URLS[new Random().nextInt(Data.URLS.length)]) // .resizeDimen(R.dimen.notification_icon_width_height, R.dimen.notification_icon_width_height) // .into(remoteViews, R.id.photo, NOTIFICATION_ID, notification); } };
private void loadImage() { // Index 1 is the progress bar. Show it while we're loading the image. animator.setDisplayedChild(1); PicassoProvider.get().load(image).fit().centerInside().into(imageView, new EmptyCallback() { @Override public void onSuccess() { // Index 0 is the image view. animator.setDisplayedChild(0); } }); } }
@Override public View getView(int position, View convertView, ViewGroup parent) { SquaredImageView view = (SquaredImageView) convertView; if (view == null) { view = new SquaredImageView(context); view.setScaleType(CENTER_CROP); } // Get the image URL for the current position. String url = getItem(position); // Trigger the download of the URL asynchronously into the image view. PicassoProvider.get() // .load(url) // .placeholder(R.drawable.placeholder) // .error(R.drawable.error) // .fit() // .tag(context) // .into(view); return view; }